/** * Clear the mutation queue. A Mutation that is currently in progress will continue to execute until finished. * */ public void clearMutationQueue() { mAppSyncOfflineMutationManager.clearMutationQueue(); } }
public void dispose(Mutation mutation) { Log.v(TAG, "Thread:[" + Thread.currentThread().getId() +"]: Dispose called for mutation [" + mutation + "]." ); appSyncOfflineMutationManager.handleMutationCancellation(mutation); }
String getClientStateFromMutation(Mutation mutation) { String clientState = ""; try { JSONObject jsonRepresentation = new JSONObject(httpRequestBody(mutation)); JSONObject variables = jsonRepresentation.getJSONObject("variables"); clientState = variables.toString(); } catch (IOException ioe) { Log.v(TAG, "IOException while getting clientState from Mutation: [" + ioe + "]"); } catch (JSONException jse) { Log.v(TAG, "IOException while getting clientState from Mutation: [" + jse + "]"); } return clientState; }
void handleMutationCancellation(Mutation canceledMutation ) { Log.v(TAG, "Thread:[" + Thread.currentThread().getId() +"]: Handling cancellation for mutation [" + canceledMutation +"]"); //Check if the mutation being cancelled is the one currently being executed. if (currentMutation != null && currentMutation.request != null && canceledMutation.equals(currentMutation.request.operation)) { Log.v(TAG, "Thread:[" + Thread.currentThread().getId() +"]: Mutation being canceled is the one currently in progress. Handling it "); setInProgressMutationAsCompleted(currentMutation.recordIdentifier); queueHandler.sendEmptyMessage(MessageNumberUtil.FAIL_EXEC); return; } //Otherwise, it is further down in the queue. Add it to the cancelled Mutations tests so that it can be handled when it reaches the front //of the queue. Log.v(TAG, "Thread:[" + Thread.currentThread().getId() +"]: Lodging mutation in cancelled mutations list "); inMemoryOfflineMutationManager.addCancelledMutation(canceledMutation); //Remove it from the persistent queue InMemoryOfflineMutationObject inMemoryOfflineMutationObject = inMemoryOfflineMutationManager.getMutationObject(canceledMutation); if ( inMemoryOfflineMutationObject != null ) { persistentOfflineMutationManager.removePersistentMutationObject(inMemoryOfflineMutationObject.recordIdentifier); } }
new PersistentOfflineMutationObject( mutationObject.recordIdentifier, httpRequestBody(mutationObject.request.operation), mutationObject.request.operation.getClass().getSimpleName(), getClientStateFromMutation((Mutation) mutationObject.request.operation)) ); Log.v(TAG,"Thread:[" + Thread.currentThread().getId() +"]: Added mutation[" + mutationObject.recordIdentifier + "] to Persistent Queue. No S3 Objects found" ); new PersistentOfflineMutationObject( mutationObject.recordIdentifier, httpRequestBody(mutationObject.request.operation), mutationObject.request.operation.getClass().getSimpleName(), getClientStateFromMutation((Mutation) mutationObject.request.operation), s3InputObjectInterface.bucket(), s3InputObjectInterface.key(),
(Mutation) request.operation, (Mutation) request.operation, appSyncOfflineMutationManager.getClientStateFromMutation((Mutation) request.operation), request.uniqueId.toString(), appSyncOfflineMutationManager); appSyncOfflineMutationManager.addMutationObjectInQueue(new InMemoryOfflineMutationObject(request.uniqueId.toString(), request, chain,
appSyncOfflineMutationManager.setInProgressPersistentMutationAsCompleted(identifier); appSyncOfflineMutationManager.setInProgressMutationAsCompleted(identifier);
@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); }
/** * Used to check if the mutation queue is empty. * @return true if queue is empty, false otherwise. */ public boolean isMutationQueueEmpty() { if (mAppSyncOfflineMutationManager != null ) { return mAppSyncOfflineMutationManager.mutationQueueEmpty(); } return true; }
appSyncOfflineMutationManager.processNextInQueueMutation();
builder.mS3ObjectManager); mAppSyncOfflineMutationManager = new AppSyncOfflineMutationManager(builder.mContext, builder.customTypeAdapters, sqlCacheOperations,
@Override public void onFailure(@Nonnull ApolloException e) { Log.v(TAG, "Thread:[" + Thread.currentThread().getId() +"]: onFailure() " + e.getLocalizedMessage()); if (e instanceof ApolloNetworkException ) { //Happened due to a network error. Log.v(TAG, "Thread:[" + Thread.currentThread().getId() +"]: Network Exception " + e.getLocalizedMessage()); Log.v(TAG, "Thread:[" + Thread.currentThread().getId() +"]: Will retry mutation when back on network"); queueHandler.setMutationInProgressStatusToFalse(); return; } shouldRetry = false; //Call the customer's callback customerCallBack.onFailure(e); //Set the mutation as completed. appSyncOfflineMutationManager.setInProgressMutationAsCompleted(recordIdentifier); //Send a message to the QueueHandler to process the next mutation in queue Message message = new Message(); message.obj = new MutationInterceptorMessage(originalMutation, currentMutation); message.what = MessageNumberUtil.FAIL_EXEC; queueHandler.sendMessage(message); return; }
@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); }
setInProgressMutationAsCompleted(currentMutation.recordIdentifier); inMemoryOfflineMutationManager.removeCancelledMutation((Mutation) currentMutation.request.operation); queueHandler.sendEmptyMessage(MessageNumberUtil.FAIL_EXEC);
@Override public void onResponse(@Nonnull ApolloInterceptor.InterceptorResponse response) { Log.v(TAG, "Thread:[" + Thread.currentThread().getId() +"]: onResponse()"); if(shouldRetry && ConflictResolutionHandler.conflictPresent(response.parsedResponse)) { //Set shouldRetry to false. Conflicts will only be attempted once. shouldRetry = false; //Found Conflict String conflictString = new JSONObject((Map)((Error) response.parsedResponse.get().errors().get(0)).customAttributes().get("data")).toString(); //Send a message to the Queue handler to retry Message message = new Message(); MutationInterceptorMessage msg = new MutationInterceptorMessage(originalMutation, currentMutation); msg.serverState = conflictString; msg.clientState = clientState; msg.requestIdentifier = recordIdentifier; msg.requestClassName = currentMutation.getClass().getSimpleName(); message.obj = msg; message.what = MessageNumberUtil.RETRY_EXEC; queueHandler.sendMessage(message); return; } //Call the customer's callback customerCallBack.onResponse(response); //Set the mutation as completed. appSyncOfflineMutationManager.setInProgressMutationAsCompleted(recordIdentifier); //Send a message to the QueueHandler to process the next mutation in queue Message message = new Message(); message.obj = new MutationInterceptorMessage(); message.what = MessageNumberUtil.SUCCESSFUL_EXEC; queueHandler.sendMessage(message); }
appSyncOfflineMutationManager.setInProgressMutationAsCompleted(persistentOfflineMutationObjectBeingExecuted.recordIdentifier); sendEmptyMessage(MessageNumberUtil.FAIL_EXEC); appSyncOfflineMutationManager.setInProgressMutationAsCompleted(inMemoryOfflineMutationObjectBeingExecuted.recordIdentifier); sendEmptyMessage(MessageNumberUtil.FAIL_EXEC);