views:

78

answers:

2

This is a big issue for me. I want to write a WPF / MVVM application that fetches data from an online WCF service.

Problem is, that the fetching process must be every, say, 15 seconds (it's a time critical application).

There is an everchanging IEnumerable involved, every time I check the WCF service, I WILL get different values, because there is also time data involved.

How would I go about that? Clearing the ObservableCollection in the ViewModel can't be right, or is it?

+1  A: 

I think it should be fine as long as it doesn't involve large changes (like thousands of items) and you manage the thread safety.

To support automatic refresh when number of items change in the collection, use ObservableCollection and to support automatic refresh on object's properties you implement INotifyPropertyChanged.

In case of large collection of items, instead of clearing the collection and filling it with new data, you can optimize this process by finding the changes between the old and new set of items and update the collection accordingly.

To manage thread safety, you will have to put any code that directly/indirectly (this includes making changes to ViewModel properties which in turn update the view) change the view in the Dispatcher.Invoke method.

decyclone
I was afraid that this was the answer... actually I was hoping for some method that stops screen refreshes while I do my updates.+1 anyway, thank you for your help! :-)
StormianRootSolver
+5  A: 

Suspending and resuming change updates on your ObservableCollection could be an approach:

See answer: http://stackoverflow.com/questions/2460557/itemscontrol-itemssource-mvvm-performance/2461929#2461929

In this way you can suspend whilst you clear down your collection and update it, then resume when you're done. This approach should mitigate issue with mangling the collection and generating lots of change update events.

chibacity
Thank you very much. I will implement this class instantly now, this is amazing. :-)
StormianRootSolver