amplify-swift
amplify-swift copied to clipboard
Is DataStore.ready always received after DataStore.syncQueriesReady?
I am having some issues updating data.
Presently I have a listener for DataStore.syncQueriesStarted, and for DataStore.syncQueriesReady and DataStore.ready
after syncQueriesStarted I don't allow data updates until DataStore.ready is called:
if event.eventName == HubPayload.EventName.DataStore.syncQueriesStarted {
print("syncQueriesStarted")
self.syncQueriesReady = false
}
if event.eventName == HubPayload.EventName.DataStore.syncQueriesReady {
print("syncQueriesReady")
self.syncQueriesReady = true
}
if event.eventName == HubPayload.EventName.DataStore.ready {
print("syncDatabaseReady")
self.databaseReady = true
self.queryDataStoreAndUpdateModelArray()
}
if event.eventName == HubPayload.EventName.DataStore.syncReceived && self.databaseReady && self.syncQueriesReady {
if let mutationEvent = event.data as? MutationEvent {
switch mutationEvent.modelName {
case "User":
self.queryUsers()
default:
self.query()
}
}
}
This generally works okay but occasionally when I come back from suspended app state, the sync queriesQueriesStarted is received but the DataStore.ready is never received.
Can you confirm if DataStore.ready will be received every time after syncQueriesStarted gets received? Should I be updating the model after syncQueriesReady? ie move self.queryDataStoreAndUpdateModelArray() to syncQueriesReady?
Not really sure what is happening or if this has to do with timeouts when internet connection intermittent?
Thanks
Also, I would like to know what happens with the datastore when my app goes into the background. Does is stay active for sometime and continue to sync? Do I need to enable background mode in app capabilities? If it stops receiving data as soon as my app enters background, then why does it not do a sync every time it enters foreground? My data will be out of sync in this case. Do I need to be calling datastore stop datastore start each time the app enters foreground? I have wasted so much time with this and would love some direction on best practices for keeping app in sync after going to background and back to foreground. Thanks.
Apologies for the delayed response. Our team will look into the issue and provide an update.
Hey @alionthego to my understanding, the .ready
event should always be received when all conditions are valid, ie. network is up and DataStore doesn't encounter any sort of catastropic errors while performing the sync. DataStore sends events to the Hub, but Amplify Hub does not guaranteed the order of events received by your listeners. In most cases there's such a delay in sending the two events when there is data to be reconciled, the order is always in the expected order.
We do expose ObserveQuery API to help customers with the use case of needing data to be consistent with the cloud is https://docs.amplify.aws/swift/build-a-backend/more-features/datastore/real-time/#observe-query-results-in-real-time . You can call this API and only display the snapshot of data once the isSynced
flag is true. Under the covers, this always leverages the .ready
event.
To answer your question, you should be reacting to the .ready
event. If something is not working as expected, please let us know if you have any repro steps.
Also, I would like to know what happens with the datastore when my app goes into the background. Does is stay active for sometime and continue to sync? Do I need to enable background mode in app capabilities? If it stops receiving data as soon as my app enters background, then why does it not do a sync every time it enters foreground? My data will be out of sync in this case. Do I need to be calling datastore stop datastore start each time the app enters foreground? I have wasted so much time with this and would love some direction on best practices for keeping app in sync after going to background and back to foreground. Thanks.
DataStore will automatically restart the sync engine to sync the latest data whenever a DataStore operation is called and DataStore's sync engine is in a stopped state. There's nothing you should need to do to have data eventually synced to the app. If the app goes to the background long enough, and comes back to the foreground, it should receive an error on the websocket connection and stop the sync engine. Any DataStore operation should kick off the sync engine again.
However, if you're looking for more consistent data as soon as possible, you can restart DataStore through the tw APIs you mentioned, when the app goes to the foreground, call DataStore.stop then DataStore.start to guaranteed that DataStore is restarted.