Skip to content

RemoveMany from a merged SourceList causes Clear #10

Description

@Nintynuts

I'm guessing it's due to some optimisation, but this test fails, and the console output shows a clear.

[TestClass]
public class DynamicDataIssues
{
	private interface ICommonInterface { }
	private struct A : ICommonInterface { }
	private class B : ICommonInterface { }

	[TestMethod]
	public void RemoveAllFromMerged()
	{
		A[] fixedItems = { new(), new() };
		SourceList<B> moreItems = new();

		var disposable = fixedItems.AsObservableChangeSet(completable: true).Transform(i => (ICommonInterface)i)
			.Merge(moreItems.Connect().Transform(i => (ICommonInterface)i))
			.Do(x => Console.WriteLine($"+{x.Adds}, -{x.Removes}, {x.First()}"))
			// The sorting is to keep things in the order I merged them, but I would prefer to not need to do this.
			.Sort(SortExpressionComparer<ICommonInterface>
				.Ascending(i => i is not A)
				.ThenByAscending(i => i is not B))
			.Bind(out var allItems)
			.Subscribe();

		Assert.AreEqual(2, allItems.Count);

		// These work as expected
		moreItems.Add(new B());
		moreItems.Add(new B());

		// But these result it the fixed items being removed
		moreItems.RemoveMany(moreItems.Items.ToArray());

		Assert.AreEqual(2, allItems.Count);

		Assert.AreEqual(2, allItems.OfType<A>().Count());
	}
}

I've worked around the issue by removing items gradially rather than all in one go, but if I do a .Clear() the whole merged collection is cleared, which is also wrong. Is there more correct way to do this?

Thanks

Update: seems likely to be coming from here:

https://github.com/reactivemarbles/DynamicData/blob/76fd915924fab0e6756038f50a4fff4464a4ed00/src/DynamicData/List/ChangeAwareList.cs#L139

Is there a better way to prevent this than to remove all items individually?

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions