RxUI doesn't work well with multiple dispatcher schedulers - ReactiveAsyncCommand fails to marshal the async work back on the correct dispatcher
RxApp.DeferredScheduler caches DispatcherScheduler.Current
When opening new windows using multiple dispatchers, the cached value of RxApp.DeferredScheduler is correct only for one of the windows.
This causes things like below to fail in certain windows
command .RegisterAsyncFunction(_ => service.GetRecords()) .Subscribe(items => collection.AddRange(items));
adding explicit call to ObserveOn(RxApp.DeferredScheduler) doesn't help either
Many classes allow you to explicitly configure the schedulers in the constructor, you most likely want to do that. Supporting > 1 dispatcher would greatly complicate the design of the code for a fairly niche scenario.
I'm going to close this bug, but if you find places where ReactiveUI doesn't let you configure the scheduler and forces you to use RxApp.DeferredScheduler, please file those bugs.
The static RxApp.DeferredScheduler is a good abstraction that I've been using to help with unit testing of view-models in virtual time. Reactive extensions support multiple dispatchers very well with DispatcherScheduler.Current.
If I can't use RxApp.DeferredScheduler I'll have to come up with some other abstration.
Could you expand a little on the 'greatly complicate the design'?
Edit: command .RegisterAsyncFunction(_ => service.GetRecords()) .ObserveOn(DispatcherScheduler.Current) .Subscribe(items => collection.AddRange(items));
The above code works when using multiple dispatchers, but I find it more difficult to test.
So, after contemplating this for awhile, if you can figure out how to map the current thread to the associated dispatcher, this should be mostly fixable. If you can do that, you could write a "Meta-Scheduler" and assign it to RxApp.DeferredScheduler that would figure out which Dispatcher to run.
Since most RxUI objects capture the scheduler in the constructor (effectively in this case making them thread affinitized on create), that should work.
Also, ping @chrisbednarski
Thanks Paul. I'll do some prototyping and put a sample up on github.
Right now, I think all the 'meta logic' is already there in DispatcherScheduler.Current
If RxApp.DeferredScheduler (or whatever static property exposes a scheduler that can be replaced for unit testing) was calling
- a
Func<IScheduler>and callDispatcherScheduler.Currentat runtime, and - the cached thread static test scheduler for unit testing
everything should be fine
I'm kinda new to Rx (RxUI even more so), so maybe am missing some other details, but the one time that a true 'Meta-Scheduler' would be required is if views and view models were created on different threads or view-models were shared between views that run on different dispatchers. If this isn't the case, then ReactiveAsyncCommand could capture the correct dispatcher scheduler by forwarding the call to DispatcherScheduler.Current whenver RxApp.DeferredScheduler is requested at runtime.
Could this conversation be continued on your google group?
If you have multiple dispatchers, the entire notion of "DispatcherScheduler.Current" is flawed isn't it? Which one will it return? Both are running concurrently!