Advice on where and when to use ObservableCollection in MvvmCross
Asked Answered
L

1

6

In an MvvmCross solution I have a singleton Service class which gets items from a web service and updates a public ObservableCollection. It does this every five seconds and items may be added or removed or their properties changed.

I also have a ViewModel which has a public property which is set to the Service's ObservableCollection. The View is bound to the ObservableCollection so that when items are added, removed or changed the view should update to show this.

However, as expected, I am getting a threading exception because the ObservableCollection is being updated by a thread other than the Main/UI one and the binding therefore cannot update the UI.

Within the Service I do not have the InvokeOnMainThread call readily available so there is no obvious cross platform way to get back on to the main thread when updating the ObservableCollection. Also, doing this just seems wrong - a Service shouldn't be concerning itself with UI matters (whereas a ViewModel can).

Also I am a bit nervous about exposing events from a Service in case this causes ViewModels to not be Garbage Collected. I note that in @slodge's N+1 series http://mvvmcross.wordpress.com/ he is using a messenging service presumably to avoid just this.

So a possible solution may be to publish a message with the latest list of items, and for the ViewModel to subscribe to the message and update its own ObservableCollection on the UI thread by comparing the message contents to it. But this seems a little clunky.

Any suggestions on the best way to implement this would be appreciated - thanks.

Lipo answered 27/8, 2013 at 12:49 Comment(0)
I
13

The original requirement that INotifyCollectionChanged must be called on the UI thread really comes from the synchronous way that the Windows controls update based upon the Added/Removed/Moved/Replaced/Reset notifications.

This synchronous update is, of course, entirely sensible - it would be very hard to update the UI display while another thread is actively changing it.

There are 'new' changes in .Net 4.5 which may mean the future is nicer... but overall these look quite complicated to me - see https://mcmap.net/q/217726/-how-do-i-update-an-observablecollection-via-a-worker-thread


The ways I know of to handle this are essentially the same as those outlined in your post:

A. keep the ObservableCollection in the Service/Model layer and marshal all events there onto the UI thread - this is possible using any class which inherits from MvxMainThreadDispatchingObject - or can be done by calling MvxMainThreadDispatcher.Instance.RequestMainThreadAction(action)

Whilst it's unfortunate that this means your Service/Model does have some threading knowledge, this approach can work well for the overall App experience.

B. make a duplicate copy of the collection in the ViewModel - updating it by some weak reference type mechanism

  • e.g. by sending it Messages which tell it what has been Added, Removed, Replaced or Moved (or completely Reset) - note that for this to work, then it's important that the Messages arrive in the correct order!

  • or e.g. allowing snapshots to be sent across from the Service/Model layer

Which of these to choose depends on:

  • the frequency, type and size of the collection changes - e.g. whether you are only getting occasional single line updates, whether you are getting frequent large bursts of changes, or whether you are mainly seeing complex groups of changes (which essentially are Resets as far as the UI is concerned)
  • the animation level required in the UI - e.g. should added/deleted items animate in/out? If no animation is required then it can sometimes be easier just to replace the entire list with a new snapshot rather than to work out the incremental changes.
  • the size of the collection itself - obviously duplicating a large collection can cause out-of-memory issues
  • the persistence required on the collection - if persistence is required, then ObservableCollection itself may not be appropriate and you may need to use a custom INotifyCollectionChanged implementation (like the MyCustomList samples)

I personally generally choose the (A) approach in apps - but it does depend on the situation and on the characteristics of the collection and its changes.

Note that while this is most definitely an mvvm issue, the underlying problem is one independent of databinding - how do you update an on-screen display of a list while the list itself is changing on a background thread?

Insulting answered 27/8, 2013 at 15:27 Comment(3)
The new EnableCollectionSynchronization looks interesting but you're right - it does look complicated, having to implement the locking is as much trouble as marshalling the events. Okay, so it looks like A IS doable after all and I guess if the ViewModel isn't handling any of its events then it won't be kept alive by being referenced from the Service. So, thanks again for a thorough answer and the tips on how to get back onto the Main thread from the Service.Lipo
I tried this one MvxMainThreadDispatcher.Instance.RequestMainThreadAction but it dosen't help :( What else could be done?Primer
#47728793 the full problemPrimer

© 2022 - 2024 — McMap. All rights reserved.