Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OldViews are empty during source collection Clear() #94

Open
Serogriff opened this issue Jan 24, 2025 · 0 comments
Open

OldViews are empty during source collection Clear() #94

Serogriff opened this issue Jan 24, 2025 · 0 comments

Comments

@Serogriff
Copy link

Serogriff commented Jan 24, 2025

Hey

I've been looking into your project for a few days already. And I discovered some behavior that was not obvious to me when I was investigating the leak. The main problem is SynchronizedView send me an empty array, when source collection was resetted.
I made a small code example to demonstrate the situation.

public sealed class ProfileViewModel : IDisposable
{
    private readonly R3.CompositeDisposable _disposable = new();
    private readonly int _id;

    public ProfileViewModel(int id)
    {
        _id = id;
    }

    public void Dispose()
    {
        Console.WriteLine($"Profile {_id} disposed.");
        _disposable.Dispose();
    }
}

private static void Main(string[] args)
{
    ObservableList<int> arr = new();
    using var view = arr.CreateView(x => new ProfileViewModel(x));
    view.ViewChanged += (in SynchronizedViewChangedEventArgs<int, ProfileViewModel> e) =>
    {
        switch (e.Action)
        {
            case NotifyCollectionChangedAction.Remove:
            {
                if (e.IsSingleItem)
                {
                    e.OldItem.View.Dispose();
                }
                else
                {
                    foreach (var vm in e.OldViews)
                    {
                        vm.Dispose();
                    }
                }

                break;
            }
            case NotifyCollectionChangedAction.Reset:
            {
                foreach (var vm in e.OldViews)
                {
                    vm.Dispose();
                }

                break;
            }
        }
    };

    arr.Add(1);
    arr.Add(2);
    arr.Add(3);

    arr.Clear(); // e.OldViews are empty in event handler

    return;
}

Now, to workaround this problem, I do

for (var i = arr.Count - 1; i >= 0; i--)
{
    arr.RemoveAt(i);
}

instead of arr.Clear(); and I get what I originally expected.

So, bug or by design?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant