I must admit that the subject of my question may sound peculiar, yet it has a rationale behind it.
I have an ObservableRangCollection<StoreSelection>
with some elements. Previously, I used .Clear()
to delete all elements and add new elements using .AddRange(...)
with elements filtered from another collection which identified the selected elements before adding them. So, the ObservableRangeCollection<StoreSelection>
had all selected elements and was bound to a CollectionView
.
I identified a negative behavior with this or let's say a "Bug". Doing this I identified increased memory consumption and rising number of registered handlers in the delegate Binding of the PropertyChanged
event. There it still kept the registered handlers after using .Clear()
and so I got more and more registered delegates increasing memory consumption and lead to a crash in the end that just closed the App (see question Rising memory consumption and rising number of Handlers in PropertyChanged event handler).
Due to this, I had to use .RemoveAt(i)
to clear all Elements within a loop instead of using .Clear()
which kind of solved the problem.
Unfortunately, I now have another negative behavior due to this. Using method .Clear()
also cleared the values in my Entrys that were bound. Now using .RemoveAt(i)
, I detected another negative behavior that I do not and did not have using .Clear()
. Using .RemoveAt(i)
, it seems like the values of my Entrys are still kind of cached and every second OnPropertyChanged(...)
shows the old values even if they cannot be seen in Debug-Mode when I look at all my values in the collection. Anyway, they appear then in the CollectionView
that is bound to this collection. I also tried to overwrite my values and again setting them to "0" for instance, but this did not help.
I tried and used .Clear()
after my .RemoveAt(i)
loop finished. Then, I can't see old values in this case anymore. But, the CollectionView
that is bound to this ObservableRangeCollection
flickers and is even empty for a short period. This is not the case when just using .RemoveAt(i)
without the .Clear()
. I also tried to use new ObservableRangeCollection<StoreSelection>()
, but this is even worse as later on all values are not bound anymore and the update and displaying the values in my Entrys does not work then anymore.
Is there a way to clear the caches of the ObservableRangeCollection
as is obviously done behind the scenes when using .Clear()
and which I could call after using .RemoveAt(i)
to delete all my elements so that my old values of bound Entrys are not showing up anymore after every 2nd OnPropertyChange(...)
call on my ObservableRangeCollection
?
Considering the link to the demo project, from your linked question:
You have:
public ObservableRangeCollection<StoreSelection> SelectedStoreItems { get; set; }
This is observable, in terms of modifying the collection's content. But it is not observable, if you change the collection itself.
You set it once, the values are shown, you can add/remove elements, it will show up. But you have to use the same object in memory.
If you want to replace the object itself:
[ObservableProperty]
public ObservableRangeCollection<StoreSelection> _selectedStoreItems;
And then you will be able to:
SelectedStoreItems = new ObservableRangeCollection<StoreSelection>()
Your case is very interesting, on top of everything you connect it to IOS memory leaks (quite a lot at the moment). This answer will only fix your binding when assigning new list.