0

I have a WPF application that uses ObservableCollection objects for binding data. The problem I am having is that I very often need to insert data at the beginning of the collection, making for a very inefficient solution. Preferably, I would like a solution that functions like an ObservableLinkedList<>, if there was an implementation in the .net library.

One thread on StackOverflow advises against this. What is a reasonable and robust solution for my problem?

Thanks

  • 2
    How do you notice that it is "inefficient"? – Clemens Mar 10 '15 at 21:39
  • Would a stack or queue work? http://stackoverflow.com/questions/3127136/observable-stack-and-queue – Pete M Mar 10 '15 at 21:41
  • What you describe is a perfect fix for implementing ReactiveExtensions. The concept is that your subscribe to an observable that does it's own thing in it's own world. When something happens it notifies those subscribers of something. The subscriber is very similar to a event handler. Lee Campbell has an excellent site for learning this found here. http://www.introtorx.com/content/v1.0.10621.0/01_WhyRx.html – JWP Mar 10 '15 at 21:42
  • @Clemens: Using something like collection.Insert(0, foo) is an O(n) (n = Count) operation, meaning that it's likely to become inefficient with large ObservableCollections. – goobering Mar 11 '15 at 13:56
  • 1
    @goobering Sure, but that was not my point. I wanted to make sure that OP has a real problem, or whether we're just talking about premature optimization. That said, you would still have to define the terms *inefficient* and *large*. But please, let's not start a discussion about that. – Clemens Mar 11 '15 at 14:42
  • @Clemens I am writing an application to visualize large amounts of data(millions of data points). The data is animated chronologically at very fast rates, meaning the ObservableCollections of data cannot manage the insertions. For every animation frame, I would prefer two O(1) operations such as `Insert` and `Remove` in order to remove one data point and add another at the opposite side of the collection. A collection such as the one I've described could easily handle seeking through the data in real time, rather than making a jump. Why create a new collection for every animation frame? –  Mar 12 '15 at 14:18
  • @CanadaIT Did you consider to create your own specialized collection class? To support WPF data binding, it would have to implement the INotifyCollectionChanged interface, which isn't overly complicated. – Clemens Mar 12 '15 at 15:06
  • @CanadaIT "millions of data points" and "very fast rates" suggests to me that data binding and an observable collection are probably not the best way to go for this, as the WPF infrastructure won't be able to cope with it, even when using virtualization for the visuals. What control were you planning on using to display the data points? – Steven Rands Mar 12 '15 at 15:33
  • @Clemens Yes, I have considered doing it that way, although in another thread, that approach was discouraged, although there wasn't much that mentioned why. –  Mar 12 '15 at 15:42
  • @StevenRands I was provided a `Visiblox` library to use for charting. I should clarify, millions of points are not plotted at the same time. Frankly, the documentation on Visiblox is pathetically absent, and I'm not sure how to handle an unknown number of data series without data binding. –  Mar 12 '15 at 15:51
  • @CanadaIT I'm not familiar with that product, but I would imagine that any charting library would struggle to refresh a data series containing that many points at a particularly fast rate. Maybe a sliding window approach would work better, whereby you work out how many points you want to display at any given time and then provide the charting control with a subset of the original data containing only that number of points. – Steven Rands Mar 12 '15 at 16:00

0 Answers0