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

Wrong notification behaviour when subscribing both with CollectionChanged and SubscribeForNotifications #3613

Open
papafe opened this issue May 29, 2024 · 2 comments

Comments

@papafe
Copy link
Contributor

papafe commented May 29, 2024

How to reproduce:

  • Use the following test:

public void ListReplace_RaisesReplaceNotifications()
{
var container = new OrderedContainer();
for (var i = 0; i < 5; i++)
{
container.Items.Add(new OrderedObject
{
Order = i
});
}
_realm.Write(() => _realm.Add(container));
var eventArgs = new List<NotifyCollectionChangedEventArgs>();
var propertyEventArgs = new List<string>();
var collection = container.Items.AsRealmCollection();
collection.CollectionChanged += (sender, e) => eventArgs.Add(e);
collection.PropertyChanged += (sender, e) => propertyEventArgs.Add(e.PropertyName!);
var oldItem = container.Items[1];
_realm.Write(() => container.Items[1] = container.Items[4]);
_realm.Refresh();
Assert.That(eventArgs.Count, Is.EqualTo(1));
Assert.That(eventArgs[0].Action, Is.EqualTo(NotifyCollectionChangedAction.Replace));
Assert.That(eventArgs[0].OldStartingIndex, Is.EqualTo(1));
Assert.That(eventArgs[0].NewStartingIndex, Is.EqualTo(1));
Assert.That(eventArgs[0].OldItems, Is.EquivalentTo(new[] { InvalidObject.Instance }));
Assert.That(eventArgs[0].NewItems, Is.EquivalentTo(new[] { container.Items[4] }));
Assert.That(propertyEventArgs.Count, Is.EqualTo(2));
Assert.That(propertyEventArgs, Is.EquivalentTo(new[] { "Count", "Item[]" }));
// Verify that modifying an object doesn't raise notifications
_realm.Write(() => container.Items[2].Order = 999);
_realm.Refresh();
// No notifications should have arrived
Assert.That(eventArgs.Count, Is.EqualTo(1));
Assert.That(propertyEventArgs.Count, Is.EqualTo(2));
}

  • Add container.Items.SubscribeForNotifications((s, c) => { }); after the other subscriptions

Result: the test does not pass because a Replace notification is called when we modify an object, and the penultimate assertion is false

Copy link

sync-by-unito bot commented May 29, 2024

➤ PM Bot commented:

Jira ticket: RNET-1152

@papafe
Copy link
Contributor Author

papafe commented May 29, 2024

I think this is due to the caching we do for notifications

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