Notification for a CBLDocument deletion

32 views
Skip to first unread message

parvez....@decurtis.com

unread,
May 23, 2017, 10:57:01 AM5/23/17
to Couchbase Mobile
For an iOS application backed by Couchbase Lite though we do have KVO support on CBLLiveQuery that helps us observe changes to the documents in database. Lets suppose if we have docs:
D1
D2
D3
and we do have a live query at iOS end to observe above documents. If I delete the document D1 from server then at iOS end, how we will be notified of this delete action? How we come to know that D1 is deleted?

Thanks

parvez....@decurtis.com

unread,
May 23, 2017, 11:21:30 AM5/23/17
to Couchbase Mobile
Isn't it possible that KVO can provide information about insert, update & delete operations so that we will be able to know that some documents are inserted, some documents are updated and some are deleted. 

Thanks

Jens Alfke

unread,
May 23, 2017, 12:01:41 PM5/23/17
to mobile-c...@googlegroups.com
On May 23, 2017, at 7:57 AM, parvez....@decurtis.com wrote:

For an iOS application backed by Couchbase Lite though we do have KVO support on CBLLiveQuery that helps us observe changes to the documents in database.

CBLLiveQuery doesn’t tell you about changes to documents. It tells you about changes to query results. That’s not the same thing: a document can change without affecting the results of a query, if value the query emits doesn’t depend on the property(ies) that changed.

If I delete the document D1 from server then at iOS end, how we will be notified of this delete action? How we come to know that D1 is deleted?

Add an observer for kCBLDatabaseChanged notifications, if you want to know about any document changes (including deletions.)
Add an observer for kCBLDocumentChanged, if you want to know about changes to a specific document.

—Jens

parvez....@decurtis.com

unread,
May 26, 2017, 10:40:52 AM5/26/17
to Couchbase Mobile
Even if I observe notification for "kCBLDatabaseChanged" still then how can we check for a document deletion case? What code do I have to write that will determine document deletion? Can you suggest a code snippet for this?

Thanks

Priya R

unread,
May 26, 2017, 11:26:38 AM5/26/17
to Couchbase Mobile
One option :
In your change listener call back function :-

 for change in notification.userInfo?["changes"] as! Array<CBLDatabaseChange> {

            if(!change.isCurrentRevision) {

                continue;

            }


            // FETCH THE DOCUMENT THAT HAS CHANGED

            let changedDoc = database.existingDocument(withID: change.documentID);

            if(changedDoc == nil) {

                // THIS WOULD TELL YOU THAT A DOCUMENT IS DELETED

                return;

Jens Alfke

unread,
May 26, 2017, 12:34:37 PM5/26/17
to mobile-c...@googlegroups.com

On May 26, 2017, at 8:26 AM, Priya R <priya.r...@couchbase.com> wrote:

            // FETCH THE DOCUMENT THAT HAS CHANGED

            let changedDoc = database.existingDocument(withID: change.documentID);

            if(changedDoc == nil) {

                // THIS WOULD TELL YOU THAT A DOCUMENT IS DELETED

                return;


It’s simpler and more efficient to just check change.isDeletion.

—Jens
Reply all
Reply to author
Forward
0 new messages