Updating a locally stored document CBL IOS

57 views
Skip to first unread message

paul Orsillo

unread,
Jan 20, 2014, 9:31:57 AM1/20/14
to mobile-c...@googlegroups.com
Hi, I have an app that receives json documents from an AFNetworking HTTPRequest. If the a doc exists I need to apply the updates and save it locally. (no CBLReplication involved)

I understand that I have to include _rev in the updated property set but I get a 409 conflict. I am missing something but don't know what that is. 
Here is what I think I need to do: 

 CBLUnsavedRevision *newRev = [tgtDoc newRevision];

 [propSet setValue:newRev forKey:@"_rev"];

 [tgtDoc putProperties:propSet error:&error];

but error returns CBLHTTP - 409

Can anyone suggest what I should be doing here? 

Thank you

Jens Alfke

unread,
Jan 20, 2014, 4:34:03 PM1/20/14
to mobile-c...@googlegroups.com

On Jan 20, 2014, at 6:31 AM, paul Orsillo <paul.o...@gmail.com> wrote:

> CBLUnsavedRevision *newRev = [tgtDoc newRevision];
> [propSet setValue:newRev forKey:@"_rev"];

This doesn't make sense. The value of a "_rev" property is a string, not a CBLUnsavedRevision object. (Remember, a document's properties are JSON, so the only types you can use are numbers, strings, NSNull, arrays and dictionaries.)

If you want to completely replace a document's properties, you can do it like this:

CBLUnsavedRevision *newRev = [tgtDoc newRevision];
newRev.userProperties = propSet;
ok = [newRev save: &error];

or a slightly better variation:

[tgtDoc update: ^(CBLUnsavedRevision* newRev) {
newRev.userProperties = propSet;
} error: &error];

The reason the second form is better is that it handles the race condition where some other thread (such as the replicator) updates the document in between the newRevision and the save, which will cause a conflict (409). The update:error: method handles this internally and starts over again.

—Jens
Reply all
Reply to author
Forward
0 new messages