Manuel Gomes
Manuel Gomes

Reputation: 21

UITableViewController and Auto-updating Results

I'm currently developing and iOS app in Swift 2.0 and I'm running into the following issue:

I have a custom query that fetches a list of items that populate a UITableView. When tapping the cell I get the ( values[indexPath.row] ) Object and segue it to another view controller for display as well as the list of items (for other actions like swipe to move to the next one).

My issue comes from a background process that fetches periodically new data and saves it to Realm.

Since Realm keeps all nice and updated, the indexPath.row will fetch a different value (the list of values has changed in the meantime due to the automatic updater) resulting in showing something that is not quite what was tapped.

Both Realm and the updater are working, but this results in a confusing experience for the user.

Am I missing something obvious?

Thanks a lot for the help!

PS: I've looked also at ABFRealmTableViewController, but I'm not displaying the whole data but a filtered (and ordered) subset.

Upvotes: 0

Views: 180

Answers (2)

marius
marius

Reputation: 7806

ABFRealmTableViewController supports filters via NSPredicate and sort descriptors as well. So you might want to give it another try.

Upvotes: 0

tilo
tilo

Reputation: 14169

Probably the easiest way of preventing this issue is to observe notifications sent out by Realm whenever a write transaction (i.e. an update) occurs and update your UI accordingly:

// Observe Realm Notifications
let token = realm.addNotificationBlock { notification, realm in
    viewController.updateUI()
}

Upvotes: 1

Related Questions