Might `documentID` become nil?

56 views
Skip to first unread message

Pascal

unread,
May 18, 2014, 11:50:38 AM5/18/14
to mobile-c...@googlegroups.com
I have the following class method, which returns a CBLQuery scoped to a given document. It does that by emitting a document-id first, ordering by that key and setting start- and stop-keys.

Now I have received a handful of crash reports from Crashlytics where this method crashes when I set the start key to a nil document ID. I cannot reproduce the crash as the method should only ever be called for a document that has been stored in the database already. So before I go nuts trying to figure out when this happens, could it be that Couchbase unsets the document's `documentID` in certain scenarios? Thanks!

+ (CBLQuery *)resultsByDateForDocument:(CBLDocument *)document

{

NSParameterAssert(document);

CBLView* view = [document.database viewNamed:@"patientResultsByDate"];

if (!view.mapBlock) {

[view setMapBlock:MAPBLOCK({

if ([@"result" isEqualToString:doc[@"type"]]) {

emit(@[doc[@"patient"], doc[@"date"] ?: [[NSDate new] med_isoRepresentation]], doc[@"_id"]);

}

}) version: @"2"];

}

NSString *docID = document.documentID; // if this is nil...

CBLQuery *query = [view createQuery];

query.descending = YES;

query.startKey = @[docID, @{}]; // ...this crashes

query.endKey = @[docID];

return query;

}

Message has been deleted

Ragu Vijaykumar

unread,
May 20, 2014, 6:53:07 AM5/20/14
to mobile-c...@googlegroups.com
From what I see, documentID is a readonly property of CBLDocument, and it doesn't seem to ever be manipulated in the class. It is passed in on initialization, so is there any chance that you created a CBLDocument with a nil docID?

CBLDocuments are cached, so perhaps you are getting a zombie CBLDocument from a document that has been evicted from the cache? You sure there is no way to for this method to be called with a dealloc'd CBLDocument that is still floating around?

Pascal

unread,
May 20, 2014, 9:50:40 PM5/20/14
to mobile-c...@googlegroups.com
Yes, this is what seems to be happening, I can't see it from the stack trace but I think this is actually coming from a CBLModel that has been deleted, and while the model instance is still around the document is gone. The assert doesn't hit in production code so this makes sense, I'll add a document.documentID check to the beginning of these methods. Thanks!

Jens Alfke

unread,
May 22, 2014, 12:50:01 PM5/22/14
to mobile-c...@googlegroups.com

On May 20, 2014, at 6:50 PM, Pascal <phase.o...@gmail.com> wrote:

Yes, this is what seems to be happening, I can't see it from the stack trace but I think this is actually coming from a CBLModel that has been deleted, and while the model instance is still around the document is gone. 

It’s an error to use a CBLModel after it’s been deleted, so make sure your app doesn’t keep any dangling references to it.

To answer the other questions that came up: A document’s ID is immutable, and it’s not possible for a document to have a nil ID. If you’re seeing document.documentID == nil, that implies that document==nil as well.

—Jens

Reply all
Reply to author
Forward
0 new messages