How to AddRange/RemoveRange in Silverlight ObservableCollection<T>?

13 במאי 2008

3 תגובות

[This blog was migrated. You will not be able to comment here.
The new URL of this post is http://khason.net/blog/how-to-addrangeremoverange-in-silverlight-observablecollectiont/]


Someone in Silverlight forum asked for interesting question: “Is there any way that I can add/remove items in bulk from an ObservableCollection object?”. The “formal” answer is: “No, AddRange RemoveRange operators are supported for List<T> only collections, thus each time you want to add or remove items from ObservableCollection, you should iterate through all items, thus CollectionChanged event will be fired each time you add or remove anything”. By the way, we have the same problem with WPF ObservableCollection<T>


Is there way to fix it? Yes, it is. However, not sure, that it is very efficient method.


image


We can subclass ObservableCollection and defer OnCollectionChanged method from firing Collectionchanged event for each add or remove items. Here how to do it


First of all create our own class, that inherits ObservableCollection<T>



public class BulkObservableCollection<T>:ObservableCollection<T>
    {


then create two methods and one member for bulk update. At the end of the update we should "tell” our collection, that it dramatically changed, thus we’ll class OnCollectionChanged with NotifyCollectionChangedAction.Reset argument.



bool deferNotification = false;
        public void AddRange(IEnumerable<T> collection)
        {
            deferNotification = true;
            foreach (T itm in collection)
            {
                this.Add(itm);
            }
            deferNotification = false;
            OnCollectionChanged(new System.Collections.Specialized.NotifyCollectionChangedEventArgs(System.Collections.Specialized.NotifyCollectionChangedAction.Reset));
        }


        public void RemoveRange(IEnumerable<T> collection)
        {
            deferNotification = true;
            foreach (T itm in collection)
            {
               this.Remove(itm);
            }
            deferNotification = false;
            OnCollectionChanged(new System.Collections.Specialized.NotifyCollectionChangedEventArgs(System.Collections.Specialized.NotifyCollectionChangedAction.Reset));
        }


Now, the only thing to do is to override OnCollectionChanged method to involve deferNotification flag



protected override void OnCollectionChanged(System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
       {
           if (!deferNotification)
           {
               base.OnCollectionChanged(e);
           }
       }


We done, so for 500 items bulk update this method works very fast, however what’s happen with 1000 items?


image


Not so good. When collection Dramatically changed renderring engine regenerates all items in bounded controls. Thus it will be almost the same time as for regular one-by-one method.


Things even worth with more, then 2000 items. If we already have 1500 items in our collection and adding another 500 items, we’ll regenerate all 2000 items by calling OnCollectionchnaged with Reset params. So it twice slower, then adding items one-by-one.


image


What to do? Work smart – add bulks when requires and single items when the collection is big. Have a nice day and be good people.


Source code for this article


הוסף תגובה
facebook linkedin twitter email

כתיבת תגובה

האימייל לא יוצג באתר. (*) שדות חובה מסומנים

3 תגובות

  1. chaiguy133730 באוקטובר 2008 ב 13:09

    When using modal modifiers like deferNotification, it is important to always wrap the reset in a "finally" block, in case an exception happens somewhere above. Otherwise, your class may be left in an inconsistent state:

    try {
    deferNotification = true;

    } finally {
    deferNotification = false;
    }

    להגיב
  2. Geert van Horrik23 בדצמבר 2008 ב 15:34

    Don't forget to check whether the collection has actually changed in the AddRange or RemoveRange (for example, what if the collection is empty)?

    I made some small modifications:

    public void AddRange(IEnumerable collection)
    {
    // Declare variables
    bool collectionChanged = false;

    // Don't update
    _deferNotification = true;

    try
    {
    // Add all items
    foreach (T itm in collection)
    {
    // Add item
    Add(itm);

    // Collection has changed
    collectionChanged = true;
    }
    }
    finally
    {
    // Notify again
    _deferNotification = false;
    }

    // Collection has changed
    if (collectionChanged)
    {
    OnCollectionChanged(new System.Collections.Specialized.NotifyCollectionChangedEventArgs(System.Collections.Specialized.NotifyCollectionChangedAction.Reset));
    }
    }

    להגיב