Performance Impact & # 8594; changing an object in the Observable Collection vs Removing and adding a new object?

I have an observable collection of a specific type T. Now I have a set of listeners for a third-party service, and I get an object T, which is actually a modification of the existing object in this observable collection.

What should I do?

1> Find the object that has been modified, and then one by one change all the properties to the newly obtained value

OR

2> just remove the object from the list and add the resulting object

Note: I can find an object from the collection by LINQ using the Id property of class T and it is unique for each object. The question is, after I find the object, will I delete it and add a new one that has the same ID, or can I change the existing one?

+3
source share
3 answers

The first solution will be the fastest.

Adding and removing an object from ObservableCollectionis pretty slow. The remove-and-add operation will increment 2 events CollectionChanged.

The problem with the first solution may be a search. You can use a more sophisticated search algorithm, or you can use a dictionary so that your objects are indexed by id.

For instance:

class ComplexObj //Maybe should implement INotifyPropertyChanged
{
    public int Id{get;set;}
    public string SomeProperty{get;set;}
}

Dictionary<int, ComplexObj> lookup = new Dictionary<int, ComplexObj>();
ObservableCollection<ComplexObj> myCollection = new ObservableCollection<ComplexObj>();

When you add an item to the collection, be sure to add it to the dictionary:

public void AddNewObj(ComplexObj obj)
{
    lookup.Add(obj.Id, obj);
    myCollection.Add(obj);
}

, :

public void Update(ComplexObj obj)
{
     lookup[obj.Id].SomeProperty = obj.SomeProperty;
}
+1

OP.

, . (), , , . , . , :

class BindableTypeThatSupportsBulkUpdate : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    private string _Text1;
    public string Text1
    {
        get { return _Text1; }
        set { _Text1 = value; NotifyPropertyChanged(); }
    }

    private string _Text2;
    public string Text2
    {
        get { return _Text2; }
        set { _Text2 = value; NotifyPropertyChanged(); }
    }

    private void NotifyPropertyChanged([CallerMemberName]string name = "")
    {
        var handler = PropertyChanged;

        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(name));
        }
    }

    public void BulkUpdate(string t1, string t2)
    {
        _Text1 = t1;
        _Text2 = t2;

        // Passing String.Empty as the name causes notification to fire for all the     properties
        NotifyPropertyChanged(String.Empty);
    }
}

, , , .

BulkUpdate , . , .

0

( @Alberto @BlackBear ):

XamGrid T, 10 , . , . - @Alberto.

3 4 , . , 2 - 3 ( , , 2 , .)

5 6 , , , .

, - , , , - , .

For me, a standard modified object has a difference of 2 to 3 properties from the actual object, and performance will be better for modifying an existing object.

0
source

All Articles