How to retrive data through database observer in Swift4?

Hello, I am new to Couchbase as well as Nosql. I can upload data on CouchBase DB and retrive them through enumerator and show them in a table view. But i want to retrive data by database observer which can observe a change on DB and i can save those data immediately in my local DB from Couchbase DB. I am on Swift4 Xcode9.1. Can anyone help me please???

Can you clarify what you mean by “local DB” ? Are you using something else other than Couchbase Lite as your local DB ?

And this is an example of how you would use database listener

 _db?.addChangeListener({ [weak self](change) in
                guard let `self` = self else {
                    return
                }
                for docId in change.documentIDs   {
                    if let docString = docId as? String {
                        let doc = self._db?.getDocument(docString)
                       
                        print("doc.isDeleted = \(doc?.isDeleted)")
                    }
                }
                
            })

Thanks for the reply i found the solution by following and actually not local DB its file system CoreData

NotificationCenter.default.addObserver(forName: NSNotification.Name.cblDatabaseChange, object: database, queue: nil) {
(notification) -> Void in
if let changes = notification.userInfo![“changes”] as? [CBLDatabaseChange] {
for change in changes {
NSLog(“Document ‘%@’ changed.”, change.documentID)
let document = self.database.document(withID: change.documentID)
var properties = document?.properties
if let id = properties?[“id”] as? String, let name = properties?[“name”] as? String, let age = properties?[“age”] as? String {

                        self.person.uniqueIDs = Int(id)
                    print(self.person.uniqueIDs ?? "i")
                    self.person.names = name
                    print(self.person.names ?? "n")
                    self.person.ages = Int(age)
                    print(self.person.ages ?? "a")
                    
                    self.core.savedObjects(id: Int(self.person.uniqueIDs), name: String(self.person.names), age: Int(self.person.ages))
                    }
                }
            }
        }

I am still not clear. Are you using Core Data in addition to CB Lite??

From code snippet above, it seems like you are using CBL 1.x ? Any reason you aren’t considering 2.0 which is in Developer Preview 21 - essentially comes down to the timeline you are targeting for your app release. The code snippet that I included is from 2.0

My task is to upload data in couch base db then if db changes listen to it and immediately save it on to core data and show data from core data. Yes! i am using 1.4.0 i don’t know why my development environment is not supporting above versions. There was pod problems. Only pods lower 1.5 is supporting.

Well- that seems like unnecessary duplication of data and kinda defeats the purpose of Couchbase Lite which is intended to be replacement for Core Data. Furthermore, you are stuck with the hassles of Core Data migrations despite having a schema less database like CBL at your disposal. So any specific reason that you still using Core Data?

CBL 2.0 is in Developer Preview . Please see instructions here
What is the timeline for your app? If it’s a few months out, I would highly recommend CBL 2.0