iOS:领域订阅状态从未更新

时间:2019-06-06 13:08:17

标签: ios swift realm subscription

我与Realm订阅观察者有关。从服务器接收数据后,状态未更新。我在下面使用此代码:

i  deploying functions, hosting
i  functions: ensuring necessary APIs are enabled...
✔  functions: all necessary APIs are enabled
i  functions: preparing prod/server directory for uploading...
Error: Error parsing triggers: Cannot find module 'acorn'
Try running "npm install" in your functions directory before deploying.

我得到的唯一状态是“ .creating”,此后没有任何其他更新。我希望获得“ .completed”,以便能够跟踪订阅获取数据的进度。 重要的是要提到我已经尝试删除这些选项,但是在这种情况下,甚至不会触发“ .creating”。

谢谢

1 个答案:

答案 0 :(得分:0)

我将用部分代码回答,因为它将提供一些指导以使此工作正常进行。假设我们有一个PersonClass,一个tableView和一个tableView数据源,称为personResults。这是在此处输入的,因此不要复制粘贴,因为我确定会有一些构建错误。

在我们的viewController中...

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

然后稍后当我们想开始同步我们的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)")
    }
}