简体   繁体   English

iOS:Realm订阅状态从未更新

[英]iOS: Realm subscription state never updated

I'm having an issue with Realm subscriptions observers. 我遇到了Realm订阅观察者的问题。 The state is not updating after receiving data from the server. 从服务器接收数据后,状态不会更新。 I'm using this code below: 我在下面使用以下代码:

        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)")}

The only state I get is ".creating" and after that nothing more is updated. 我得到的唯一一个状态是“.creating”,之后就没有更新了。 I would like to get ".completed" to be able to track progress of the subscription getting data. 我希望“.completed”能够跟踪订阅获取数据的进度。 Important to mention that I already tried to remove the options but in this case even ".creating" is not triggered. 重要的是,我已经尝试删除选项,但在这种情况下甚至不会触发“.creating”。

Thanks 谢谢

I will answer with some partial code as it will provide some directon to get this working. 我将回答一些部分代码,因为它将提供一些直接的工作。 Assume we have a PersonClass, a tableView and a tableView dataSource called personResults. 假设我们有一个PersonClass,一个tableView和一个名为personResults的tableView数据源。 This was typed here so don't just copy paste as I am sure there are a couple of build errors. 这是在这里输入的,所以不要只是复制粘贴,因为我确定有一些构建错误。

In our viewController... 在我们的viewController中......

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

then later when we want to start sync'ing our personResults 然后我们想要开始同步我们的人结果

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