Amplify-js: DataStore: How to know when sync is finished

Created on 12 Mar 2020  路  4Comments  路  Source: aws-amplify/amplify-js

* Which Category is your question related to? *
DataStore
* What AWS Services are you utilizing? *
AppSync
* Provide additional details e.g. code snippets *
How can we know that DataStore has finished the sync?
When doing the first await DataStore.query(MyEntity) after user logged in, DataStore is returning right away and not waiting for the list to be sync with the cloud (500 objects needs to be downloaded from dynamo). We need to wait all entities for certain part of the application and we want to put a loading when we don't have all the informations (only for the first synchronization).
We noticed that a lastFullSync field is filled in the "sync_ModelMetadata" indexDB when it's done and is set to null when no sync has been done but this is not exported and we couldn't find anything in the DataStore that can help us with that...
How can we get this information? (We have a react and react-native application connected to the same endpoint).

Thanks,
Sebastien

DataStore feature-request

All 4 comments

@SebSchwartz sync is a continuous process, it doesn't necessarily start or stop at a specific time. For instance if you are online events are entering the system via subscriptions. On the other hand when you transition from an offline to online mode there is a 3-way merge that happens between potential mutations in the offline queue, delta sync, and incoming subscriptions. Any mutations that have an error such as from a conflict or otherwise (authorization, etc.) will fire a callback for you to decide what to do locally on the data. Since you can observe on the items with an observable you shouldn't need to wait for anything. Could you give more details on the specifics of the use case/scenario you're trying to account for? Perhaps we could do something with emitting events to Hub for you to leverage but getting more information would help.

@undefobj Here is a simplified version of our graphql schema:

type House 
  @model
 {
  id: ID!
  name: String!
  rooms: [Room] @connection(keyName: "byHouse", fields: ["id"])
}

type Room 
  @model
  @key(name: "byHouse", fields: ["houseId"]){
  id: ID!
  name: String!
  type: String!
  houseId: ID!
  house: House @connection(fields: ["houseId"])
}

There is other fields and stuffs but I removed them for explaining the problem we are facing and why we want to know when the first sync is over.

In the frontend, we needs to be able to perform house.rooms. Unfortunately, we are not receiving House.rooms from DataStore, 1-to-many and many-to-many relationships are not resolved when querying the object (only Room.house is resolved). Before going with DataStore we were using appsync with offline (which you know is no more updated due to apollo changes) and one single graphql query gave us all the information we needed.
So we now have build the whole object ourselves in the frontend (with the *-many relationships). The problem is that we need to know when the sync is over to be able to do that.
I hope it is making more sense on why we want to know when it鈥檚 finished.

@SebSchwartz We have a feature request open for this - https://github.com/aws-amplify/amplify-js/issues/4808. Closing this issue and we would be tracking the updates for the feature on the mentioned issue. If you have more info that could help us, please feel free to add that in the issue.

Lets keep this issue open until #4808 is addressed

Was this page helpful?
0 / 5 - 0 ratings