Long execute()

in aws-android-sdk-appsync/src/main/java/com/amazonaws/mobileconnectors/appsync/AWSAppSyncDeltaSync.java [175:255]


    Long execute(final boolean forceFetch) {
        //Initialize the Delta Sync Machinery if required.
        initializeIfRequired();

        if (cancelled) {
            Log.v(TAG, "Delta Sync: Cancelled. Quitting Delta Sync process for id [" + id + "]");
            return id;
        }

        this.deltaSyncOperationFailed = false;
        new Thread( new Runnable() {
            @Override
            public void run() {
                final CountDownLatch baseQueryCountdownLatch = new CountDownLatch(1);
                Log.v(TAG, "Delta Sync: Starting Sync process");

                //Run base query from the cache
                runBaseQuery(AppSyncResponseFetchers.CACHE_ONLY);
                if (deltaSyncOperationFailed ) {
                    scheduleRetry();
                    return;
                }

                //Subscribe
                if (subscription != null ) {
                    mode = QUEUING_MODE;
                    subscribe();
                    if (deltaSyncOperationFailed) {
                        scheduleRetry();
                        return;
                    }
                }

                //Check if we need to execute the DeltaQuery or BaseQuery from the network.
                boolean executeBaseQuery = true;
                if (!forceFetch && deltaQuery != null) {
                    //Check if
                    long deltaInSeconds = (System.currentTimeMillis() - (lastRunTimeInMilliSeconds - 2000)) / 1000;
                    Log.v(TAG, "Delta Sync: Time since last sync [" + deltaInSeconds + "] seconds");
                    if (deltaInSeconds < baseRefreshIntervalInSeconds) {
                        Log.v(TAG, "The last baseQuery from NETWORK was executed less than [" + baseRefreshIntervalInSeconds + "] seconds ago. Will run DeltaQuery from network");
                        executeBaseQuery = false;
                    }
                    else {
                        Log.v(TAG, "The last baseQuery from NETWORK run was before [" + baseRefreshIntervalInSeconds + "] seconds. Will run BaseQuery from network");
                    }
                } else {
                    Log.v(TAG, "Will run BaseQuery from network");
                }

                if (executeBaseQuery) {
                    runBaseQuery(AppSyncResponseFetchers.NETWORK_ONLY);
                } else {
                    runDeltaQuery();
                }

                if (deltaSyncOperationFailed) {
                    scheduleRetry();
                    return;
                }

                //Propagate all messages received so far and put subscription in processing mode
                synchronized (processingLock) {
                    Log.v(TAG, "Delta Sync: Delta query completed. Will propagate any queued messages on subscription");
                    Response r;
                    while ((r = messageQueue.poll()) != null) {
                        if (subscriptionCallback != null ) {
                            Log.v(TAG, "Delta Sync: Propagating queued message on subscription");
                            subscriptionCallback.onResponse(r);
                        }
                    }
                    Log.d(TAG, "Delta Sync: All queued messages propagated. Flipping mode to PROCESSING");
                    mode = PROCESSING_MODE;
                }

                retryAttempt = 0;
            }
        }).start();

        return id;
    }