Skip to content

ObservableList with View causes duplication of Move notification #118

@pixsperdavid

Description

@pixsperdavid

When using an ObservableList with a view, the move event gets duplicated. This causes issues with synchronized UI controls (Avalonia in my example) which then move their items around twice causing invalid states.

I'm working on a fix for this in a fork, can open a PR to merge following, however I haven't had much response from Cysharp lately.

private static void TestObservableList()
{
    var values = new ObservableList<int> { 0, 1, 2, 3 };
    
    var valuesView = values
        .ToNotifyCollectionChanged();

    int moveEventCount = 0;
    
    valuesView.CollectionChanged += (sender, e) =>
    {
        if (e.Action == NotifyCollectionChangedAction.Move)
            moveEventCount++;
    };
    
    values.Move(0, 1);
    
    Debug.Assert(moveEventCount == 1);
}

private static void TestObservableListWithView()
{
    var values = new ObservableList<int> { 0, 1, 2, 3 };
    
    var valuesView = values.CreateView(i =>  i)
        .ToNotifyCollectionChanged();

    int moveEventCount = 0;
    
    valuesView.CollectionChanged += (sender, e) =>
    {
        if (e.Action == NotifyCollectionChangedAction.Move)
            moveEventCount++;
    };
    
    values.Move(0, 1);
    
    // Fails, always == 2
    Debug.Assert(moveEventCount == 1);
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions