couchbaselabs/CouchCocoa — pull request #50 by lambmj (2012-10-11 14:59:40+00:00)
Title: For bulkSave, update properties[_rev] from response

Recently, while optimizing my code, I found that performing bulk creates [database putChanges:] dramatically improved the performance when saving multiple documents to my local TouchDB instance. However, I encountered a problem. Following the bulk save, the _rev property of the documents are nil and the model derived from the document cannot execute save. Interestingly, the currentRevisionID property of the document is correct even though _rev is nil.

Here's is my code that retrieves the model after the bulk operation.

```
- (id)getModelWithID:(NSString *)id {
CouchDocument *document = [self.database documentWithID:id];
NSLog(@"_rev = %@", [document propertyForKey:@"_rev"]);
NSLog(@"currentRevisionID = %@", document.currentRevisionID);
return [[CouchModelFactory sharedInstance] modelForDocument:document];
}
```

Here's the output when executed for a document created with putChanges:

2012-10-10 11:56:25.520 MyApp[16231:2c3f] _rev = (null)

2012-10-10 11:56:25.521 MyApp[16231:2c3f] currentRevisionID = 1-00defd215f94b792ee0ad0c87ed22a14

A subsequent save generates this output:

2012-10-10 11:56:25.522 MyApp[16231:2c3f] WARNING: Trying to PUT to CouchDocument[3e65..42f2] without specifying a rev ID

2012-10-10 11:56:25.542 MyApp[16231:2c3f] WARNING: One: Save failed: Error Domain=CouchDB Code=409 "The operation couldn’t be completed. conflict" UserInfo=0x11020c80 {NSUnderlyingError=0x11020ed0 "conflict", NSLocalizedFailureReason=conflict}

2012-10-10 11:56:25.543 MyApp[16231:2c3f] save failed, op.error = Error Domain=CouchDB Code=409 "The operation couldn’t be completed. conflict" UserInfo=0x11020c80 {NSUnderlyingError=0x11020ed0 "conflict", NSLocalizedFailureReason=conflict}

Fwiw, I see the document on my remote CouchDB instance with a valid _rev property before this code executes. I have verified that the currentRevisionID (1-00defd215f94b792ee0ad0c87ed22a14) matches the _rev property of the document in my remote CouchDB instance. If I kill and restart my app, the document and model pick up the correct setting of _rev and everything works properly:

2012-10-10 12:17:29.358 MyApp[18125:570f] _rev = 1-00defd215f94b792ee0ad0c87ed22a14

2012-10-10 12:17:29.359 MyApp[18125:570f] currentRevisionID = 1-00defd215f94b792ee0ad0c87ed22a14

If I remove the bulk save (putChanges:, getModelWithID:) and instead create and save the models (alloc, init, putProperties:, save) one at a time, this problem does not occur.

Fwiw, I updated to the latest CouchCocoa and TouchDB-iOS before performing these tests. I'm looking at the code in CouchCocoa to see if I can spot the problem. Seems like it should be something fairly simple but as I'm not familiar with that code, I thought I would post here first.