Synchronous emission of the initially fetched values #28
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR restores a very useful behavior that has been lost after the transition to RxSwift schedulers in v0.8.0, which is the guarantee, for database observables that target the main thread, of the synchronous emission of the initially fetched database values.
This feature is very important for view controllers that subscribe to database values in their viewDidLoad() method and rely on synchronous view setup:
Since v0.8.0, this useful behavior has been provided by the good old MainScheduler.instance.
But this is not enough.
Applications also need, sometimes, to update the database and then take immediate action without waiting for the database change notification that will eventually come (changes notifications are always asynchronously dispatched).
For example, an app may need to insert a row in the database, update the view, and then select and scroll to the matching row in a table view.
This use case is difficult when the delivery of updated database values is asynchronous. It requires heavy Rx setup in order to provide specific handling of the expected database change notification, only this one, and not others (considering any background thread may also perform database changes in the mean time).
It is not only difficult, but also error-prone. It's very easy to do it in a wrong way.
Conversely, this use case can be handled in a much easier way by simply resubscribing to the database content right after the database change. Here is the suggested setup:
Did you notice the two "Here the view has been updated" comments above? The first one was already guaranteed. The second one is only guaranteed by this PR.
To achieve this behavior, RxGRDB no longer defaults to MainScheduler.instance.
To understand why MainScheduler.instance is not a correct fit, let's analyse what happens in the
doStuff
method:This is not a bug of MainScheduler.instance, which makes very sure that scheduled items are executed in the same order as they are scheduled. It just happens that this ordering goes against the interest of the scenario detailed above: when an app needs synchronous notification, it needs to opt out from the strict scheduler ordering.
Applications that rely on strict ordering can still provide an explicit scheduler, as below. But they lose synchronous notifications: