Search code examples
performancexamarin.formsinotifypropertychanged

Improving performance of INotifyPropertyChanged affecting all items in a collection


I have an ObservableCollection that's used as the ItemsSource of a Xamarin Forms DataGrid. The view models in the collection each have an observable IsSelected property, bound to a Switch in the data grid's row template. There can be something up to ~20k rows in this data grid.

There are buttons for "Select All" and "Select None", which do what you'd expect: they set this property to the appropriate value for each element in the collection. For large collections, this takes multiple seconds to complete, and the reason is handling all of those property change notifications to keep the UI in sync.

Is there a way to either batch these change notifications, or suspend them and update the UI the old-fashioned way, or do something else, that will improve the performance? Techniques I've seen for batching PropertyChanged notifications involve coalescing multiple changes to the same property on the same object, which is not a problem I have.


Solution

  • Option 1:

    We always use pagination if we need to load large numbers of items . In your case you could update the next ten or more items' IsSelected before we add them to ItemsSource .

    Option 2:

    You can simply run multiple methods at a time. So that you could update the value of IsSelected in multiple methods (in each method you could update 1000 or more items)

    // this process run synchronously.
    Task.Run(() =>
    {
    // call your methods
    MyFunction1(); // update 1-1000
    MyFunction2(); // update 1001-2000 
    });
    
    // this process doesn't wait for function execution,it will run Asynchronous way.use this if you dont want to use function return value;
    Task.Factory.StartNew(MyFunction1);
    Task.Factory.StartNew(MyFunction2);
    

    Both process help you to non blocking UI.