How to check for a document's existence in local DB

473 views
Skip to first unread message

Ajith

unread,
Mar 27, 2014, 3:03:24 PM3/27/14
to mobile-c...@googlegroups.com
I was trying to find out the existence of a document with ID (say, 'abcd') in a database and I went with -existingDocumentWithID:. But it doesn't returns me nil when a document with ID doesn't exists in local DB(I cross checked using CouchbaseLiteViewer app for Mac).  Replacing -existingDocumentWithID: with  -existingLocalDocumentWithID: always returns me nil, even when document with ID does exists on local database. Could you please guide me here? How can I check existence of document with ID?

Thanks!

Traun Leyden

unread,
Mar 27, 2014, 4:11:20 PM3/27/14
to mobile-c...@googlegroups.com
I just tried the following unit test on couchbase lite android and it worked as expected.  Are you saying the equivalent code on iOS is not working?  (I find that hard to believe)

    public void testGetNonExistentDocument() throws CouchbaseLiteException {
        Document existingDocument = database.getExistingDocument("422klj2lk2j4l34lkj");
        assertNull(existingDocument);
    }


On Thu, Mar 27, 2014 at 12:03 PM, Ajith <ajith...@icloud.com> wrote:
I was trying to find out the existence of a document with ID (say, 'abcd') in a database and I went with -existingDocumentWithID:. But it doesn't returns me nil when a document with ID doesn't exists in local DB(I cross checked using CouchbaseLiteViewer app for Mac).  Replacing -existingDocumentWithID: with  -existingLocalDocumentWithID: always returns me nil, even when document with ID does exists on local database. Could you please guide me here? How can I check existence of document with ID?

Thanks!

--
You received this message because you are subscribed to the Google Groups "Couchbase Mobile" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mobile-couchba...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/mobile-couchbase/819f68b4-5503-415e-b449-c1457e71f865%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Jens Alfke

unread,
Mar 27, 2014, 7:15:54 PM3/27/14
to mobile-c...@googlegroups.com

On Mar 27, 2014, at 12:03 PM, Ajith <ajith...@icloud.com> wrote:

> I was trying to find out the existence of a document with ID (say, 'abcd') in a database and I went with -existingDocumentWithID:. But it doesn't returns me nil when a document with ID doesn't exists in local DB

Hm … yes, there might be a bug in the iOS implementation. It looks like if you’ve previously accessed the document with that ID (which doesn’t exist), then a future call to existingDocumentWithID: will return that cached CBLDocument object. I should fix that.

> How can I check existence of document with ID?

Check whether document.currentRevision != nil.

—Jens

Jens Alfke

unread,
Mar 27, 2014, 9:09:19 PM3/27/14
to mobile-c...@googlegroups.com

On Mar 27, 2014, at 4:15 PM, Jens Alfke <je...@couchbase.com> wrote:

Hm ... yes, there might be a bug in the iOS implementation. It looks like if you've previously accessed the document with that ID (which doesn't exist), then a future call to existingDocumentWithID: will return that cached CBLDocument object. I should fix that.

Just checked in a fix, btw. (Only in the master branch for now, but it’ll be in 1.0.)

—Jens

Ajith

unread,
Mar 28, 2014, 2:37:51 AM3/28/14
to mobile-c...@googlegroups.com
Oops! My bad, I should have mentioned iOS. Thanks for replying B.T.W :)

Ajith

unread,
Mar 28, 2014, 2:57:57 AM3/28/14
to mobile-c...@googlegroups.com
 It looks like if you’ve previously accessed the document with that ID (which doesn’t exist), then a future call to existingDocumentWithID: will return that cached CBLDocument object.
 
I suspected that & checked it. Even the very first call to -existingDocumentWithID:, without calling -documentWithID previously was returning document object.

Check whether document.currentRevision != nil. 
 
That works. Thanks! 

Ajith

unread,
Mar 28, 2014, 3:48:49 AM3/28/14
to mobile-c...@googlegroups.com
Just checked in a fix, btw. (Only in the master branch for now, but it’ll be in 1.0.)
We are still using beta 2(from stable branch), will be upgrading soon.  Started back with this project this week and I've few questions based on observations.

1. Why would I ever hit following error? I observed this with two views. How can I prevent this(or any) crash in Views?
 *** ASSERTION FAILED: Cannot reindex view 'type/project' which has no map block set
 *** Assertion failure in -[CBLView(Internal) updateIndex](), /Users/Ajith/couchbase-lite-ios/Source/CBLView+Internal.m:184

2. I'm observing kCBLReplicationChangeNotification to update UI(involves querying database) when replication status hits kCBLReplicationIdle & kCBLReplicationStopped(since persistence replications=true). Nothing wrong here until I delete a database. Selector method -replicationProgress receives notification for deleted database soon after deleting a database and triggered Update UI query crashes. I wrote following piece of code in -replicationProgress and it fixed. 

- (void)replicationProgress:(NSNotificationCenter *)notify {


CBLReplication *repl = castIf(CBLReplication, [notify valueForKey:@"object"]);

CBLDatabase *db = [self.manager existingDatabaseNamed:repl.localDatabase.name error:nil];

if (!db)

  return;

//remaining good stuff here . .

}


Question here is, why would replication send a notification when database doesn't exists in local? 

Jens Alfke

unread,
Mar 28, 2014, 9:53:46 AM3/28/14
to mobile-c...@googlegroups.com

On Mar 28, 2014, at 12:48 AM, Ajith <ajith...@icloud.com> wrote:

> 1. Why would I ever hit following error? I observed this with two views. How can I prevent this(or any) crash in Views?
> *** ASSERTION FAILED: Cannot reindex view 'type/project' which has no map block set
> *** Assertion failure in -[CBLView(Internal) updateIndex](), /Users/Ajith/couchbase-lite-ios/Source/CBLView+Internal.m:184

The view’s being queried before you’ve registered a map block. To be safe, always register your map/reduce blocks right after opening the database.

> Question here is, why would replication send a notification when database doesn't exists in local?

Because of a bug, I guess :( But CBLReplication was cleaned up a lot in beta 3. If you can still reproduce this after upgrading, please file a bug report.

—Jens

Reply all
Reply to author
Forward
0 new messages