Hi Scooty,
> So I created a ThreadedReactiveCollection, which invokes changes to itself
> to the dispatcher.
This is fundamentally a Bad Idea™ though many people want it, and let me tell
you why.
Consider the following scenario, that you want to write your own version of
CreateDerivedCollection - you want to take the initial state + the
notifications and mirror the current state of the collection.
If you look at the definition of NotifyCollectionChangedEventArgs, it is
effectively a "delta" on the current state. Meaning, that to get the new
state, you must combine the old state + the delta. This implies though, that
you must receive these deltas **in order** so that you can reconsitute the new
state.
Running these change notifications through a scheduler fundamentally violates
that requirement - there is no guarantee that scheduler items will arrive in
the same order that they happen. This is a dealbreaker for any code that wants
to use these notifications.
> So I'm trying to figure out where I've gone wrong. Is it my design, my
> implementation, or is there a magic trick I don't know?
So, in general, the idea is that collections that you bind to in the UI (even
indirectly via CreateDerivedCollection) should only be changed on the UI
thread.
So, how can you solve this problem? So, say we have our network method to
fetch new Models:
IObservable<List<Model>> GetTheModels();
and our ReactiveCollection:
ReactiveCollection<Model> VisibleModels;
GetTheModels()
.ObserveOn(RxApp.DeferredScheduler)
.Subscribe(x = >{
using (VisibleModels.SuppressChangeNotifications()) {
VisibleModels.Clear();
VisibleModels.AddRange(x);
}
});
This can result in significantly better perf as well, as this strategy
generates the minimum amount of change notifications (=> the minimum number of
XAML layouts / loading new controls / etc).
> Lastly, while I understand that ReactiveCollection is derived from
> ObservableCollection
It's actually not anymore, it's fully reimplemented. While this was a ton of
work, it got us a lot of benefits around correctly issuing notifications in
certain scenarios.
--
Paul Betts <
pa...@paulbetts.org>