in aws-android-sdk-appsync/src/main/java/com/amazonaws/mobileconnectors/appsync/AppSyncOfflineMutationInterceptor.java [453:579]
public void interceptAsync(@Nonnull final InterceptorRequest request,
@Nonnull ApolloInterceptorChain chain,
@Nonnull Executor dispatcher,
@Nonnull final CallBack callBack) {
//Check if this is a mutation request.
if (!(request.operation instanceof Mutation)) {
//Not a mutation. Nothing to do here - move on to the next link in the chain.
chain.proceedAsync(request, dispatcher, callBack);
return;
}
Log.v(TAG,"Thread:[" + Thread.currentThread().getId() +"]: Processing mutation.");
Log.v(TAG,"Thread:[" + Thread.currentThread().getId() +"]: First, checking if it is a retry of mutation that encountered a conflict.");
//Mutations that go through the conflict resolution handler will be present in the mutationsToRetryAfterConflictResolution map.
if (!mutationsToRetryAfterConflictResolution.containsKey(request.operation)) {
Log.v(TAG,"Thread:[" + Thread.currentThread().getId() +"]:Nope, hasn't encountered conflict");
//Create a callback to inspect the results before calling caller provided callback.
ApolloInterceptor.CallBack customCallBack = new InterceptorCallback(
callBack,
queueHandler,
(Mutation) request.operation,
(Mutation) request.operation,
appSyncOfflineMutationManager.getClientStateFromMutation((Mutation) request.operation),
request.uniqueId.toString(),
appSyncOfflineMutationManager);
try {
//Add the custom callback to the in memoryInterceptorCallback map
callbackMapForInMemoryMutations.put(request.uniqueId.toString(), customCallBack);
//Hand off to the appSyncOfflineMutationManager to do the work.
appSyncOfflineMutationManager.addMutationObjectInQueue(new InMemoryOfflineMutationObject(request.uniqueId.toString(),
request,
chain,
dispatcher,
customCallBack));
} catch (Exception e) {
Log.e(TAG, "ERROR: "+ e);
e.printStackTrace();
}
return;
}
//This is a mutation that is being retried after the conflict resolution handler has addressed the conflict.
Log.d(TAG, "Thread:[" + Thread.currentThread().getId() +"]: Yes, this is a mutation that gone through conflict resolution. Executing it.");
//Remove from the map. We only do one retry.
mutationsToRetryAfterConflictResolution.remove(request.operation);
//Get the callback associated with the first attempt of this mutation.
Log.v(TAG, "Looking up originalCallback using key[" + request.operation.toString() + "]");
InterceptorCallback callbackForInMemoryMutation = (InterceptorCallback) callbackMapForInMemoryMutations.get(request.operation.toString());
if (callbackForInMemoryMutation != null ) {
Log.v(TAG, "callback found. Proceeding to execute inMemory offline mutation");
//Execute inMemory Mutation
chain.proceedAsync(request,dispatcher,callbackForInMemoryMutation);
return;
}
// Original callback was null. This is due to the mutation being a persistent offline mutation which is being retried
final PersistentMutationsCallback callbackForPersistentMutation =
appSyncOfflineMutationManager.persistentOfflineMutationManager.networkInvoker.persistentMutationsCallback;
final PersistentOfflineMutationObject object = persistentOfflineMutationObjectMap.get(request.operation.toString());
Log.d(TAG, "Thread:[" + Thread.currentThread().getId() +"]: Fetched object: " + object);
chain.proceedAsync(request, dispatcher, new CallBack() {
@Override
public void onResponse(@Nonnull InterceptorResponse response) {
callBack.onResponse(response);
if ( callbackForPersistentMutation != null) {
JSONObject jsonObject;
try {
jsonObject = new JSONObject(response.clonedBufferString.get());
callbackForPersistentMutation.onResponse(new PersistentMutationsResponse(
jsonObject.getJSONObject("data"),
jsonObject.getJSONArray("errors"),
request.operation.getClass().getSimpleName(),
object.recordIdentifier));
} catch (Exception e) {
callbackForPersistentMutation.onFailure(new PersistentMutationsError(
request.operation.getClass().getSimpleName(),
object.recordIdentifier,
new ApolloParseException(e.getLocalizedMessage()))
);
}
}
appSyncOfflineMutationManager.setInProgressPersistentMutationAsCompleted(object.recordIdentifier);
queueHandler.clearInMemoryOfflineMutationObjectBeingExecuted();
queueHandler.clearPersistentOfflineMutationObjectBeingExecuted();
queueHandler.sendEmptyMessage(MessageNumberUtil.SUCCESSFUL_EXEC);
}
@Override
public void onFetch(FetchSourceType sourceType) {
callBack.onFetch(sourceType);
}
@Override
public void onFailure(@Nonnull ApolloException e) {
callBack.onFailure(e);
if ( callbackForPersistentMutation != null) {
callbackForPersistentMutation.onFailure(
new PersistentMutationsError(request.operation.getClass().getSimpleName(),
object.recordIdentifier,
e));
}
appSyncOfflineMutationManager.setInProgressPersistentMutationAsCompleted(object.recordIdentifier);
queueHandler.clearPersistentOfflineMutationObjectBeingExecuted();
queueHandler.clearInMemoryOfflineMutationObjectBeingExecuted();
queueHandler.sendEmptyMessage(MessageNumberUtil.FAIL_EXEC);
}
@Override
public void onCompleted() {
}
});
}