Home > Blockchain >  How to sync enqueued data before getting data from aws amplify
How to sync enqueued data before getting data from aws amplify

Time:03-09

On my app log in process, I have a service that get the latest datas from aws amplify DataStore Events

private String processName = "Checking network status...";

private SubscriptionToken subscriptionToken;

public void sync() {

    AmplifyDataStoreManager.start();

    subscriptionToken = Amplify.Hub.subscribe(
            HubChannel.DATASTORE,
            hubEvent -> DataStoreChannelEventName.NETWORK_STATUS.toString().equals(hubEvent.getName()) ||
                    DataStoreChannelEventName.SUBSCRIPTION_DATA_PROCESSED.toString().equals(hubEvent.getName()) ||
                    DataStoreChannelEventName.MODEL_SYNCED.toString().equals(hubEvent.getName()) ||
                    DataStoreChannelEventName.READY.toString().equals(hubEvent.getName()),
            hubEvent -> {

                Log.d("DataStore - Hub Event Name: "   hubEvent.getName());

                if (hubEvent.getData() != null) {
                    Log.d("DataStore - Hub Event Data: "   hubEvent.getData());
                }

                if (DataStoreChannelEventName.NETWORK_STATUS.toString().equals(hubEvent.getName())) {

                    NetworkStatusEvent networkStatusEvent = (NetworkStatusEvent) hubEvent.getData();

                    if (networkStatusEvent != null && !networkStatusEvent.getActive()) {

                        onProcessError("Device not connected to internet");

                    }

                } else if (DataStoreChannelEventName.SUBSCRIPTION_DATA_PROCESSED.toString().equals(hubEvent.getName())) {

                    ModelWithMetadata modelWithMetadata = ((ModelWithMetadata) hubEvent.getData());

                    ModelMetadata modelMetadata = modelWithMetadata.getSyncMetadata();

                    Log.d("DataStore - Model ID: "   modelWithMetadata.getModel().getId());

                    Log.d("DataStore - Model Name: "   modelWithMetadata.getModel().getModelName());

                    processName = "Syncing "    modelWithMetadata.getModel().getModelName()   "...";

                    EventBus.getDefault().post(new ProcessEvent(this));

                    if (TextUtils.equals(AmplifyDataModel.Transaction.name(), modelWithMetadata.getModel().getModelName()) && (modelMetadata.isDeleted() == null || !modelMetadata.isDeleted())) {

                        AppAmplifyDataAccessManager.saveAppTransaction(AppAmplifyConfiguration.getDataSyncManager().mapAppTransaction(modelWithMetadata.getModel()));

                    } else if (TextUtils.equals(AmplifyDataModel.Configuration.name(), modelWithMetadata.getModel().getModelName()) && (modelMetadata.isDeleted() == null || !modelMetadata.isDeleted())) {

                        AppAmplifyConfiguration.getDataSyncManager().mapConfigurations(modelWithMetadata.getModel());
                    }

                } else if (DataStoreChannelEventName.MODEL_SYNCED.toString().equals(hubEvent.getName())) {

                    ModelSyncedEvent modelSyncedEvent = (ModelSyncedEvent) hubEvent.getData();

                    if (modelSyncedEvent != null) {

                        processName = "Syncing "    modelSyncedEvent.getModel()   "...";

                        EventBus.getDefault().post(new ProcessEvent(this));

                    }

                } else {

                    Amplify.Hub.unsubscribe(subscriptionToken);

                    onProcessCompleted();
                }

            }
    );
}

Configuration Model data sample:

double grandTotal;

aws configuration: grandTotal = 100;
local configuration: grandTotal = 100;

The scenario is, the device is offline, did transaction, local configuration: grandTotal is now 200. The sync configuration is called and enqueued because the device is offline. Closed the app, the internet is back, opens the app, in the login process, sync() method is called. What happens is the local configuration: grandTotal is 100 again, because of sync(), then after that, the enqueued sync when the device is offline runs(but I was not able to debug this, its not going to the sync method of configuration). And the result is:

aws configuration: grandTotal = 200;
local configuration: grandTotal = 100;

What I want is if there is a pending sync, do that first before syncing aws data to local.

CodePudding user response:

After reading DataStoreChannelEventName enum descriptions, I tried adding OUTBOX_STATUS and fixed my problem, I'm not sure how but I tested all the scenario and its working.

else if (DataStoreChannelEventName.OUTBOX_STATUS.toString().equals(hubEvent.getName())){

                    OutboxStatusEvent outBoxStatusEvent = (OutboxStatusEvent) hubEvent.getData();

                    if (outBoxStatusEvent != null) {

                        Log.d("DataStore - Hub Event Data: "   hubEvent.getData());

                    }
  • Related