[iOS] Ordering of changes during replication

HI all,

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:

2016-12-11T18:19:21.471Z CRUD: Stored doc "MOD_40021AC9595340EA918C358C930CB107" / "1-f4ad63f0d97009dbaaaa28b641c5e069"
2016-12-11T18:19:21.691Z CRUD: Stored doc "PD_2016_12_12" / "11-f06f24fb1eafbe7d6aafa5e0889fced8"

However when I pull from another client I get the documents in reverse order:

2016-12-11T18:19:22.405Z HTTP:  #143: GET /ketodiet/PD_2016_12_12?rev=11-f06f24fb1eafbe7d6aafa5e0889fced8&revs=true&attachments=true&atts_since=%5B%2210-2d3279e6e5099259e59cc2497aa114c0%22%5D  (as 1)
2016-12-11T18:19:22.503Z HTTP:  #144: GET /ketodiet/MOD_40021AC9595340EA918C358C930CB107?rev=1-f4ad63f0d97009dbaaaa28b641c5e069&revs=true&attachments=true  (as 1)

I’m trying to enforce correct ordering so that leaf documents are created / synced first before documents that use them.

How do I do that?

Thank you!

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.

Thanks again.

@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.


1 Like

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.