繁体   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