简体   繁体   中英

iOS: Realm subscription state never updated

I'm having an issue with Realm subscriptions observers. 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. I would like to get ".completed" to be able to track progress of the subscription getting data. Important to mention that I already tried to remove the options but in this case even ".creating" is not triggered.

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. This was typed here so don't just copy paste as I am sure there are a couple of build errors.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM