couchbaselabs / CouchCocoa

Objective-C API for CouchDB on iOS and Mac OS
http://couchbaselabs.github.com/CouchCocoa/docs/
218 stars 66 forks source link

update _rev on bulk create (putChanges:) and update (putChanges:toRevisions:) #49

Closed lambmj closed 12 years ago

lambmj commented 12 years ago

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.

lambmj commented 12 years ago

Closed the previous issue and reopened this pull request. Sorry for the noob churn.