簡體   English   中英

優化添加/刪除/更新集合中新項目的方式

[英]Optimize way of adding/removing/updating a new item in a collection

我想問一下我是否仍然可以優化代碼,因為目前,這就是我編寫代碼的方式。

這是通過使用MVVM,ObservableCollection和C#進行的

這是偽代碼

// ClientCollection is an ObservableCollection<T> object
// ClientLists is a List<T> object that came from the JSON data
//
// check our clients in ClientCollection
// if a client was removed in List<T>, then delete this client
// in our ClientCollection.
ToRemoveLists toremove;
{ 
    for each Clients client in ClientCollection
        var a = from b in ClientLists
                where b.name == client.name
                select b;

        if a.count() == 0 
            toremove.Add a;
}
{ // remove client from ClientCollection
    for each Clients client in toremove
        ClientCollection.Remove client;
}

// now add new clients if there are any
// or update client info if this client exists
{ 
    for each Clients client in ClientLists
        var a = from b in ClientCollection
                where b.name == client.name
                select b;

        if a.count() == 0 
            ClientCollection.Add clients
        else
            // update client info
            // .
            // .
            // .
}

之所以這樣做,是因為使用.Clear()似乎不是更新集合的好方法,首先它刷新整個列表,當您有成千上萬個項目時,它似乎在閃爍。.所以這就是我這樣做的原因。

表示一堆

有很多事情可以提高性能,但這是一個很好的起點,可以解決您的問題。 繼承自ObservableCollection並提供進行批量更改的方法,但僅引發單個事件,從而減少綁定更新:

public class BulkObservableCollection<T> : ObservableCollection<T>
{
    public BulkObservableCollection()
    {
    }

    public BulkObservableCollection(List<T> list) : base(list)
    {
    }

    public BulkObservableCollection(IEnumerable<T> collection) : base(collection)
    {
    }

    /// <summary>
    /// Adds a range if items to the collection, causing a reset event to be fired.
    /// </summary>
    /// <param name="items"></param>
    public void AddRange(IEnumerable<T> items)
    {
        if (items == null) throw new ArgumentNullException("items");

        var intialCount = Items.Count;
        Items.AddRange(items);            
        if (Items.Count != intialCount)
            OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
    }

    /// <summary>
    /// Removes a range of items from the collection, causing a reset event to be fired.
    /// </summary>
    /// <param name="items"></param>
    public void RemoveRange(IEnumerable<T> items)
    {
        if (items == null) throw new ArgumentNullException("items");

        var intialCount = Items.Count;
        foreach (var item in items)            
            Items.Remove(item);
        if (Items.Count != intialCount)
            OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
    }

    public void Reset(IEnumerable<T> newItems)
    {
        if (newItems == null) throw new ArgumentNullException("newItems");

        Items.Clear();
        Items.AddRange(newItems);

        OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM