簡體   English   中英

如何使用 couchbase 在 Swift4 中通過數據庫觀察器檢索數據?

[英]How to retrive data through database observer in Swift4 with couchbase?

我是 Couchbase 和 Nosql 的新手。 我可以在 CouchBase DB 上上傳數據並通過枚舉器檢索它們並在表視圖中顯示它們。 但我想通過觀察者檢索數據,觀察者可以觀察到數據庫的變化,我可以立即將這些數據從 Couchbase DB 保存在我的本地數據庫中。 我在 Swift4 Xcode9.1 上。 有人可以幫我嗎???

通過以下數據庫更改可以在 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))
                    }
                }
            }
        }

嘗試在獲取數據時使用以下方法,以便檢測數據庫中的更改。

制作一個如下所示的功能:

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()

    }

另請參閱此 入門套件

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM