new ObservableCollection vs adding items in a loop
Asked Answered
U

1

6

In terms of speed and the amount of notifications generated, is this code:

ObservableCollection<Foo> foo = new ObservableCollection<Foo>(bar);
this.SomeProperty = foo;

the same as:

this.SomeProperty = new ObservableCollection<Foo>();

foreach (var v in bar) 
{
    this.SomeProperty.Add(v);
}

If they are the same, is it possible to somehow turn off the notifications generated?

Objective: I'm trying to speed up the display of Telerik RadChart in silverlight. It seems to take a while to display (and freezes the in browser app) even after the property containing ObservableCollection is set. Once the chart is drawn everything works correctly.

Unmoving answered 1/2, 2012 at 15:37 Comment(0)
G
8
  1. Profile it or Test it! According to the docs, the PropertyChanged event occcurs when an item is added, removed, changed, moved, or the entire list is refreshed. You can therefore write some test code that just subscribes to this event and see what happens.

  2. Fast performing and thread safe observable collection - if it is due to a constant barrage of OnChanged events, consider only firing after a bulk update – someone has done this work for you already

  3. http://www.telerik.com/help/silverlight/radchart-performance-tips-and-tricks.html deals specifically with the scenario you describe about half way down the page. Their conclusion is the same as 2 - in fact, the code looks very similar :-)

If the freeze is occuring before the binding actually takes place, then I'd make sure that the delay isn't actually rendering based or because of another actvity (like the time taken to load the collection). Again, profiling is your friend.

Giffin answered 1/2, 2012 at 15:50 Comment(1)
The event won't be fired during the constructor, as it's impossible to subscribe to it beforehand (and thus it's useless to fire it, as there will definitely be no listeners).Dandridge

© 2022 - 2024 — McMap. All rights reserved.