The initial requirement that INotifyCollectionChanged should be called in the user interface thread really comes from the synchronous way to update Windows updates based on notifications with Add / Remove / Move / Replace / Reset.
This synchronous update, of course, is perfectly reasonable - it would be very difficult to update the display of the user interface, while another thread is actively changing it.
There are βnewβ changes in .Net 4.5 that may mean that the future will be more pleasant ... but overall they look rather difficult for me - see fooobar.com/questions/107401 / ...
The ways I know for this are basically the same as in your post:
a. keep the ObservableCollection at the Service / Model level and place there all the events in the user interface thread - this is possible using any class that inherits from MvxMainThreadDispatchingObject , or can be done by calling MvxMainThreadDispatcher.Instance.RequestMainThreadAction(action)
Although this is bad, it means that your service / model has some knowledge of streaming, this approach may work well for the overall experience with applications.
C. Duplicate the copy of the collection in the ViewModel - updating it using some weak reference type mechanism
eg. By sending them messages that tell him what was added, deleted, replaced or moved (or completely Reset) - note that this requires that the messages arrive in the correct order!
or, for example, allowing you to transfer pictures from the Service / Model level
Which one to choose depends on:
- the frequency, type, and size of changes to the collection β for example, whether you receive periodic updates on a single line, whether you receive frequent large spurts of changes, or if you see mostly complex groups of changes (which are essentially
Resets in relation to the user interface). - animation level required in the user interface. Should animate in / out elements be added / removed? If animation is not required, it can sometimes be easier to simply replace the entire list with a new snapshot rather than handle incremental changes.
- the size of the collection itself - obviously duplicating a large collection can cause memory problems.
- persistence required for the collection - if persistence is required, then the
ObservableCollection itself may not be acceptable, and you may need to use a custom implementation of INotifyCollectionChanged (for example, MyCustomList samples )
Usually I usually choose approach (A) in applications, but it depends on the situation and on the characteristics of the collection and its changes.
Note that although this is definitely a mvvm problem, the main problem is not dependent on data binding - how do you update the screen display of a list while the list itself changes to a background thread?
source share