Is there a way to tell when AWS Amplify Datastore is initialized or ready to be queried?

Viewed 541

I have an application that needs to update the UI with the results of an Amplify Datastore query. I am making the query as soon as the component mounts/renders, but the results of the query are empty even though I know there is available data. If I add a timeout of 1 second or greater before making the query, then the query returns the expected data. My hunch is that this is because the query is returning an empty set of data before the response from the delta sync table, which shows there is data to be fetched, is returned.

Is there any type of event provided by Datastore that would allow me to wait until the data store is initialized or has data to query before making the query?

I understand that I could use the .observe functionality of datastore for a similar effect, but this is currently not an option.

1 Answers

First, if you do not use the Datastore start method then sync from the backend starts when the first query is submitted. Queries are run against the local store so data won't be there yet.
Second, Datastore publishes events on the amplify hub so that you can monitor changes, such as a set of data being synced, Datastore being ready and even Datastore being ready and all data synced locally.

See the documentation on Datastore.start and the documentation for Datastore events for more information.

Related