I’m saving a document (id: PD_xxx) which has a collection of other documents (id: MOD_xxx). I make sure I create the dependant document first (MOD_xxx) and save it before adding it to PD_xxx and then saving again.
On the sync gateway the changes (Push) arrive in the right order:
There isn’t a way to guarantee the replication ordering of documents. However if doc B references doc A I understand you might want to wait for doc A to be replicated before querying doc B.
When doc B arrives, you could use a document change listener on doc A’s _id to know when it arrives?
I’m not sure if there is another way to do it without being too app specific. @jens any ideas?
As a distributed system, Couchbase Mobile can’t preserve ordering of updates, nor can it provide transactions. This does make some things trickier.
If you’re writing reactive code that updates the UI (or other dependent state) as documents change, then your app will be eventually-consistent. In the case you describe, if a leaf document hasn’t been pulled yet, your code will see an empty document at first; then when the leaf arrives, you’ll update with the pulled revision and display the correct data.
Thank you for clarifying that - we’ll make changes to ensure we handle this correctly. Actually, in iOS due to the forgiving nature of obj-c this is not an issue (we get some transitional blank elements) - it’s only an issue for Android where we plan to have default empty instances for non-existent (or yet to be synced) leaf documents.
@nkonstas Keep us in the loop on how the implementation for iOS/Android goes. It’s a frequently asked question that we’d like to cover in the docs and having some code snippets from an actual app built with CBM would be really helpful to cover this concept.
Sure I will do James - we currently Beta test the iOS app with syncing disabled but in a few days I’ll push a build with syncing on - Anyone can try the Beta here (https://ketodietapp.com/Forums/ForumView.aspx?forumId=1)
I’m happy to provide any snippets and explain how we implemented things. If anything any feedback on our implementation will be beneficial for us too.
Just a follow up - our beta version with sync enabled is now available to download - anyone that wants to try it and see how syncing works in our app just check out the info here: https://ketodietapp.com/Forums/ForumView.aspx?forumId=1
I’m happy to explain how we implemented things and provide code snippets.