Closed PawKanarek closed 1 year ago
Thanks for the repro! While we don't guarantee 100% identical behavior with ObservableCollection
, this does look like a bug and we'll investigate.
Thanks. @papafe I've also noticed, that if i manually trigger realm.Refresh()
after replacing items, then nothing happens, but when i trigger it after items.Clear()
then i'm receiving new extra event Remove
. Maybe this will help
realm.Write(() => colllection.Items.Add(item2)); // Add items
realm.Write(() => colllection.Items.Move(item1, 2)); // Move items
realm.Write(() => colllection.Items.Remove(item0)); // Remove items
realm.Write(() => colllection.Items[1] = item2); // Replace items (don't work)
realm.Refresh(); // NEW CODE COMPARING TO SNIPPET - does nothing
realm.Write(() => colllection.Items.Clear()); // Reset items (after realm.Refresh(), generates new remove action)
realm.Refresh(); // NEW CODE COMPARING TO SNIPPET - adds new "remove" action
Output:
Actions for ObservableCollection: Add, Move, Remove, Replace, Reset
Actions for RealmCollection: Add, Move, Remove, Remove
^Thats extra Remove
action comparing to previous snippet
For me right now the missing Replace
action is biggest issue. Thank you for investigating :)
@PawKanarek I managed to reproduce the issue, thanks a lot for your detailed example! I just had to copy paste 😄
Regarding your main issue, the main problem here is that we do not raise the CollectionChanged
event when the object gets replaced, and that's something we need to fix.
Regarding the behaviour you've noticed in the second message, that is expected. There are actually two things happening here:
Realm.Refresh()
updates the realm to the latest version and also forces the sending of all outstanding notifications. This is something that happens organically when using realm on the main thread of an application, without the need to call Refresh()
manually. In your case what was happening is that before you added the last Refresh
call, the notification for Clear()
was not being sent out yet. The penultimate call to Refresh
was actually computing the notification, but because of the bug I've said before nothing was raised.ObservableCollection
, but we try to respect the meaning of the NotifyCollectionChangedAction
enum. Because Clear()
just removes the content of the collection we raise Remove
as it complies with the meaning of the enum. We try to resort to Reset
only in extreme cases when we cannot raise any other event. We can probably special case the situation where the collection count is 0 and raise Reset
as it's probably going to be faster for the UI to redraw the content.
Thanks for investigating so fast :) Yes, i also think that Reset
is not that important, because we have Remove
events, so that's good. Also I agree that RealmCollecetion don't need to be exactly the same as ObservableCollection. I've created this bug mosty for Replace
event, because my UI wasn't responding when items changed order in Realm Database.
What happened?
Hi, I've noticed a bug where RealmCollection doesn't behave in the same way as ObservableCollection. From what I've observed, RealmCollection doesn't fire
Replace
&Reset
from INotifyCollectionChanged Interface, thus my app is not updating the UI correctly when I switched from ObservableCollection to RealmDb in my ViewModels. I've made a very simple example to prove my point.Repro steps
Launch provided code snippet in console application.
Version
net5.0 for console app & Xamarin for my main project issue
What SDK flavour are you using?
Local Database only
What type of application is this?
Xamarin
Client OS and version
Android, iOS, ConsoleApp in .net core Console app for macOS
Code snippets
Relevant log output
^ ObservableCollection & RealmCollection should fire INotifyCollectionChanged events in the same way