Search code examples
dynamic-datareactiveui

Move from IReactiveDerivedList to DynamicData


Has simple code in old version of ReactiveUI:

var allItems = new ObservableCollection<Model>(items);
var filteredItems = allItems.CreateDerivedCollection(
 x => x,
 Filter,
 Comparer.Compare);

where Filter and Compare has simple signatures:

private bool Filter(Model item)
public int Compare(Model x, Model y)

sometimes i change items in other threads (big changes, without INPC) or change Filter\Compare strategies and just do filteredItems.Reset();


In DynamicData i try to:

          ReadOnlyObservableCollection<Model> filteredItems;
          var allItems = new ObservableCollection<Model>(items);
          var cancellation = allItems
            .ToObservableChangeSet()
            .Filter(Filter)
            .Sort(Comparer)
            .ObserveOn(SynchronizationContext.Current)
            .Bind(out filteredItems)
            .DisposeMany()
            .Subscribe();

but not found, how to Reset this^ or filteredItems.


Solution

  • Wish I were able to give you a direct answer, but I'm rather new to ReactiveUI. Managed to use DynamicData for something and thought you wouldn't mind this sharing.

    If what I say later doesn't help you, here are the most relevant resources I could find:


    In my case I used SourceList or SourceCache as type for something similar to your allItems.

    SourceCache<Model, string> allItems =
        new SourceCache<Model, string>(m => m.Id);
    
    // assuming each model has unique id; if it doesn't then use SourceList
    

    I'd then have a BindingList<Model> as type for something simlar to your 'filteredItems'.

    BindingList<Model> filteredItems = new BindingList<Model>();
    

    The binding should be something like:

    allItems
        .Connect()
        ...
        .ObserveOn(...)
        .Bind(filteredItems)
        .Subscribe();
    

    To bulk-edit the list I'd call something like

    allItems.Edit(
        innerList => {
        innerList.Clear();
    
        // edit
        // innerList.AddOrUpdate(...);
    });
    

    Cheers !