I am new to Couchbase as well as Nosql. I can upload data on CouchBase DB and retrieve them through enumerator and show them in a table view. But i want to retrieve data by 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???
By following database change can be observed in Swift4
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))
}
}
}
}
Try to use this below method while getting data so it will detect changes in DB.
Make one function like below:
fileprivate func addLiveQueryObserverAndStartObserving() {
guard let liveQuery = liveQuery else {
return
}
// 1. iOS Specific. Add observer to the live Query object
liveQuery.addObserver(self, forKeyPath: "rows", options: NSKeyValueObservingOptions.new, context: nil)
// 2. Start observing changes
liveQuery.start()
}
Also refer this starterkit
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.