如何在从 aws amplify 获取数据之前同步排队的数据
How to sync enqueued data before getting data from aws amplify
在我的应用程序登录过程中,我有一项服务可以从 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();
}
}
);
}
配置模型数据示例:
double grandTotal;
aws 配置:总计 = 100;
本地配置:grandTotal = 100;
场景是,设备离线,做交易,本地配置:grandTotal现在是200。由于设备离线,同步配置被调用并入队。
关闭应用程序,互联网恢复,打开应用程序,在登录过程中,调用sync()方法。发生了什么是本地配置:grandTotal 再次为 100,因为 sync(),然后,设备离线时排队的同步运行(但我无法调试它,它不会进入配置的同步方法).结果是:
aws 配置:总计 = 200;
本地配置:grandTotal = 100;
我想要的是如果有待同步,在将 aws 数据同步到本地之前先执行此操作。
阅读 DataStoreChannelEventName enum 描述后,我尝试添加 OUTBOX_STATUS 并解决了我的问题,我不确定如何但我测试了所有场景及其工作。
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());
}
在我的应用程序登录过程中,我有一项服务可以从 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();
}
}
);
}
配置模型数据示例:
double grandTotal;
aws 配置:总计 = 100;
本地配置:grandTotal = 100;
场景是,设备离线,做交易,本地配置:grandTotal现在是200。由于设备离线,同步配置被调用并入队。 关闭应用程序,互联网恢复,打开应用程序,在登录过程中,调用sync()方法。发生了什么是本地配置:grandTotal 再次为 100,因为 sync(),然后,设备离线时排队的同步运行(但我无法调试它,它不会进入配置的同步方法).结果是:
aws 配置:总计 = 200;
本地配置:grandTotal = 100;
我想要的是如果有待同步,在将 aws 数据同步到本地之前先执行此操作。
阅读 DataStoreChannelEventName enum 描述后,我尝试添加 OUTBOX_STATUS 并解决了我的问题,我不确定如何但我测试了所有场景及其工作。
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());
}