Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Writing on the same thread as notifiers results in a temporarily inconsistent state #6588

Open
sync-by-unito bot opened this issue May 8, 2023 · 0 comments

Comments

@sync-by-unito
Copy link

sync-by-unito bot commented May 8, 2023

The intended design for change notifications is that other than inside a write transaction, it should be impossible to ever read a new value from an object without first receiving a change notification related to that new value. This is very important for things like UITableView which have their own internal tracking of the row count - if the results of a query changes without a notification being sent first, UITableView may request a row index which is now out of bounds.

Currently when a write is performed on the same thread as notifications are registered on, the write is applied to the read state immediately, and then notifications are asynchronously sent later. This leaves a window where the new state can be read before the notification is sent. With UITableView this happens to work out fine most of the time, but if two writes are performed in a row, the refresh triggered by the first write often happens before the notifications for the second write are sent.

Performing writes on a background thread avoids this, but is sometimes difficult to do.

There's two potential solutions I can think of:

  1. Synchronously send notifications immediately after committing a write transaction. This is the easiest to understand thing and would eliminate some existing user confusion. Unfortunately, it means doing a large amount of blocking work on the UI thread, which can often take longer than the write itself.
  2. Don't advance the read transaction version when committing a write. We could commit the write transaction but roll back the Transaction to the initial read version, resulting in the writes not being visible until the next refresh happened. This would make on-thread writes behave almost exactly like off-thread writes, which is a nice bit of consistency. It's probably also extremely surprising and confusing to the overwhelming majority of users. KVO would need to send rollback notifications.
  3. Expose an API to give the user the option to do either of the above.

These are both easy to implement but seem bad. Hopefully a better solution is possible.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

0 participants