realm-swift: Add write transactions which do not produce local notifications

The current fine-grained notifications approach makes it challenging to support scenarios where the UI is updated before the data model, rather than the reactive approach of the model being the canonical source of truth that the UI merely displays. The simplest motivating use-case is UITableView reordering; when using the built-in functionality for dragging rows around the delegate is merely notified of changes already displayed in the UI, and re-applying those changes in the collection notification block after the Realm is committed will corrupt the order to the UITV. Because notifications are coalesced, users can’t reasonably filter out these redundant updates themselves. and even if they could it would be very easy to get wrong.

We should expose a variant of Realm.write(_:) that would specify that the transaction should not produce notifications on the current thread, as the things they want to update with the notifications are already up-to-date. In addition, this variant of write transactions would block until all async notifications are ready and delivers them, since otherwise the UI could easily get “out of sync”.

An alternative approach would be to instead let the user pass in the tokens for notification blocks which they want to suppress. This is more flexible and makes it work with commits on a different thread from the notified thread, but maybe would make the API even more complex.

@tgoyne has implemented a proof of concept of this in #3439, and there’s more discussion in that thread, but that work is currently on hold.

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Comments: 16 (14 by maintainers)

Most upvoted comments

Indicating which tokens to skip can be done on commit, which makes the behavior a lot less weird. Doing it on begin was only necessary for the logic of conditionally blocking for previous notifications.

Is there any info about when this will be available?

It’s at least a few weeks away, maybe longer. I do think that @JadenGeller wants to start working on this soon.

RefreshRequired isn’t sent for writes on the current thread, as the Realm is inherently already at the latest version when it’s committing a write transaction.