Maintain document between offline & online mode

14 views
Skip to first unread message

parvez....@decurtis.com

unread,
Aug 24, 2017, 2:12:55 PM8/24/17
to Couchbase Mobile
For a couchbase lite (1.3.1) based iOS application (that can start in offline mode initially but can switch to online mode later on), how we can maintain same document when moving from offline to online mode. What I want to achieve is that for two users with ids user1 and user2 logged in on two different iOS devices. Both these users are required to maintain a single document that both of them can update. The problem is that the document is created on both of the devices dynamically. Since users are dynamically created based on registration so we cannot pre-populate the document in a database and simply update it. Is it possible to write logic in sync function on sync gateway such that it will check the incoming documents and based on type will re-assign a documentt ID to the document such as DOC::user1:user2 so both documents from user1 and user2 can be merged into a single document and gets updated back to both of the devices.



Is it possible to assign documentID to a document in sync function?
Is there any other way to solve this issue?

Thanks

Jens Alfke

unread,
Aug 25, 2017, 12:10:50 PM8/25/17
to mobile-c...@googlegroups.com


On Aug 24, 2017, at 11:12 AM, parvez....@decurtis.com wrote:

Is it possible to write logic in sync function on sync gateway such that it will check the incoming documents and based on type will re-assign a documentt ID to the document such as DOC::user1:user2 so both documents from user1 and user2 can be merged into a single document and gets updated back to both of the devices.

I'm not sure I'm understanding what you want. But document IDs are immutable. Changing the document ID isn't possible because it would result in a different document.

If two users will be editing the same document, they need to both know the ID of that document. You could consider defining its ID as a function of the two users' IDs, i.e. "shared-between-$A-$B" where $A and $B are the two user IDs. (To make this independent of which user is A and which B, you should choose A as the ID thats sorts lower, i.e. min(user1,user2), and B as the one that sorts higher.)

—Jens

parvez....@decurtis.com

unread,
Aug 26, 2017, 2:53:58 AM8/26/17
to Couchbase Mobile
if I sort the userIDs before generating the custom documentID for the document then I think it will create same documentIDs on both devices (in offline mode), for example userids as 100 & 200, we can create a documentID as DOCID:100:200 after sorting the ids in descending order. I think even if we create document with this approach on two devices still then Couchbase lite can handle the conflicts resulting from sync (when in online mode), correct?

Jens Alfke

unread,
Aug 27, 2017, 5:09:38 PM8/27/17
to mobile-c...@googlegroups.com


On Aug 25, 2017, at 11:53 PM, parvez....@decurtis.com wrote:

I think even if we create document with this approach on two devices still then Couchbase lite can handle the conflicts resulting from sync (when in online mode), correct?

Yes, it's a special type of conflict, where you have two initial (gen-1) revisions and two roots of the rev tree. But you can detect & resolve it like any other conflict.

—Jens
Reply all
Reply to author
Forward
0 new messages