Couchbase lite, sync_gateway Observing to Remote only Synchronized Changes to Documents

Dear Team,

I want ot observer only to remote changes ( not local) ?

I build ios swift 4 calendar events application that have notifications for each event. if i set event locally
i then set notification for this event. but i need to set the notification
for each event synced from remotes …

Please advice

It’s usually better to structure your code so all GUI updates for doc changes are driven by Couchbase Lite change notifications, even changes made locally. It’s usually simpler, and reduces the chance of error.

In 1.x there was a flag on change notifications to tell which were remote, but 2.x doesn’t have it.

Doing the GUI update twice usually won’t be noticeable. If it is a problem, you can remember the doc’s sequence property after you update, and check it again in the notification handler: if it hasn’t changed, neither has the document.

can please provide a snippet how to compare documents sequence property in background
while sync is in continues mode ?

Thank You in advance.

if (doc.sequence !=_ lastSequence) {
   _ lastSequence = doc.sequence;

in db.update i set

let doc = self._db?.document(withID: id)
 data["lastSequence"] = doc?.sequence
var mutableDoc = MutableDocument.init(id:id,data:data)

Then i set un observer for db changes
but get different sequences for local updates :

fileprivate func registerForDatabaseChanges() {
        dbChangeListenerToken = db?.addChangeListener({ [weak self](change) in
            guard let `self` = self else {
            for docId in change.documentIDs   {

                if let docString = docId as? String {
                    let doc = self._db?.document(withID: docString)
                    if doc == nil {
                        print("Document was deleted: \(docString)")
                    else {
                        if let dict = doc!.toDictionary() as? [String:Any],
                            let type = dict["type"] as? String {
                                if(type == "event"){
                                    print("got event")


(I cleaned up the formatting of your post by adding lines of 3 back-quotes around the code blocks.)

var mutableDoc = MutableDocument.init(id:id,data:data)

Not related to this thread, but that line seems wrong. That constructor is for creating a new document, but you’re using it with the same ID as the existing document. That’s a recipe for conflicts. I think what you want is a mutable copy of doc? In that case, call doc.toMutable().

Then i set un observer for db changes but get different sequences for local updates :

Each update will change the sequence. What should be happening is:

  1. You save a change to the document (sequence changes to 999, say)
  2. You use your own notification system to update the GUI (or other state)
  3. Your update code draws the GUI or updates other state, and caches the documents sequence, 999
  4. Now your CBL change listener [the code you posted] gets called; it also calls your update code
  5. Your update code checks the document’s sequence, 999, and sees that it’s equal to its cached sequence, so it does nothing.

But then, if I have say 1000 local events I need to “cache” them all aka create separate entry in another database for each event I have to store it sequence Id. Since I cannot cache it in event model itself course I get senquence Id after the update …

You can cache the sequence number as part of the data of the view.

Or you can just skip updating your view after you make a change, and let the database-change listener drive the update, which is a cleaner design.

Im using MVVM and have event, user, group models…

Where is the best practice to put database-change listener ?

Currently it is nit separate Couchedb file


ok that will do for me

Thank You very Much !