Cysharp / Cysharp/ObservableCollections

ObservableList with View causes duplication of Move notification

Open
#118 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C#
Stars
1k
Forks
73
Avg merge
5d 4h
Merged PRs (30d)
2

Description

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);
}

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start from ObservableList.Move, CreateView, and ToNotifyCollectionChanged, then reproduce the two snippets from the issue. Trace the Move notifications when a view is present and verify that the synchronized collection receives exactly one NotifyCollectionChangedAction.Move event.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
frontend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.