簡體   English   中英

iOS:Realm訂閱狀態從未更新

[英]iOS: Realm subscription state never updated

我遇到了Realm訂閱觀察者的問題。 從服務器接收數據后,狀態不會更新。 我在下面使用以下代碼:

        let realm = try! Realm(configuration: try configuration(url: realmURL))
        let results: Results<Object> = realm!.objects(Object.self)
        let subscription = results.subscribe(named: "objects")


        subscription.observe(\.state, options: .initial) { state in
        print("Sync State Objects: \(state)")}

我得到的唯一一個狀態是“.creating”,之后就沒有更新了。 我希望“.completed”能夠跟蹤訂閱獲取數據的進度。 重要的是,我已經嘗試刪除選項,但在這種情況下甚至不會觸發“.creating”。

謝謝

我將回答一些部分代碼,因為它將提供一些直接的工作。 假設我們有一個PersonClass,一個tableView和一個名為personResults的tableView數據源。 這是在這里輸入的,所以不要只是復制粘貼,因為我確定有一些構建錯誤。

在我們的viewController中......

class TestViewController: UIViewController {
   let realm: Realm
   let personResults: Results<Person>
   var notificationToken: NotificationToken?
   var subscriptionToken: NotificationToken?
   var subscription: SyncSubscription<Project>!

然后我們想要開始同步我們的人結果

subscription = personResults.subscribe()
subscriptionToken = subscription.observe(\.state, options: .initial) { state in
    if state == .complete {
        print("Subscription Complete")
    } else {
        print("Subscription State: \(state)")
    }
}

notificationToken = personResults.observe { [weak self] (changes) in
    guard let tableView = self?.tableView else { return }
    switch changes {
    case .initial:
        // Results are now populated and can be accessed without blocking the UI
        print("observe: initial load complete")
        tableView.reloadData()
    case .update(_, let deletions, let insertions, let modifications):
        // Query results have changed, so apply them to the UITableView
        tableView.beginUpdates()
        tableView.insertRows(at: insertions.map({ IndexPath(row: $0, section: 0) }),
                             with: .automatic)
        tableView.deleteRows(at: deletions.map({ IndexPath(row: $0, section: 0)}),
                             with: .automatic)
        tableView.reloadRows(at: modifications.map({ IndexPath(row: $0, section: 0) }),
                             with: .automatic)
        tableView.endUpdates()
    case .error(let error):
        // An error occurred while opening the Realm file on the background worker thread
        fatalError("\(error)")
    }
}

暫無
暫無

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

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