couchbaselabs / CouchCocoa

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

Deleted document causes crash when using getDocumentsWithIDs #67

Open lambmj opened 11 years ago

lambmj commented 11 years ago

My code:

CouchQuery *query = [self.database getDocumentsWithIDs:@[@"E12906BA-3BCD-4E1D-8297-20917C85BC1D"]];
CouchQueryRow *row = [query.rows nextRow];
NSLog(@"row = %@", row); 

crashes on the last line with this error when retrieving a deleted document from CouchDB:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSNull objectForKey:]: unrecognized selector sent to instance 

The cause appears to be that CouchQueryRow doesn't check to see if document is null before attempting to retrieve properties from it in this (and other) code in CouchQuery.m

- (NSString*) documentID {
    NSString* docID = [[_result objectForKey: @"doc"] objectForKey: @"_id"];
    if (!docID)
        docID = [_result objectForKey: @"id"];
    return docID;
}

The raw query retrieves rows for deleted documents even if include_deleted is false:

curl -X POST 'http://user:password@couchdemo:5984/channel-c/_all_docs?include_docs=true&update_seq=true&include_deleted=false' -H 'Content-Type: application/json' -d '{ "keys":["E12906BA-3BCD-4E1D-8297-20917C85BC1D"]}'
{"total_rows":48,"update_seq":2995,"offset":0,"rows":[
{"id":"E12906BA-3BCD-4E1D-8297-20917C85BC1D","key":"E12906BA-3BCD-4E1D-8297-20917C85BC1D","value":{"rev":"14-816a350e91526ad90043b11baebe78eb","deleted":true},"doc":null}
]}

It's not clear to me if the best fix is to patch the calls and add a deleted property to CouchQueryRow or if CouchQuery should not return this row in the first place.

snej commented 11 years ago

Is this talking to a CouchDB server, not TouchDB?

The JSON returned by the query is weird — I didn't think the doc property could be null.

snej commented 11 years ago

Also, I don't think I've ever heard of an include_deleted query param. It's not described anywhere in the CouchDB wiki.

lambmj commented 11 years ago

Yes, it's a CouchDB 1.2.1 server. I saw the include_deleted in a post on Stack Overflow. It didn't matter -- I got the same results either way