Xamarin.Forms icon indicating copy to clipboard operation
Xamarin.Forms copied to clipboard

[Bug] [iOS] Setting CollectionView.ItemsSource to null after selecting an item crashes

Open GalaxiaGuy opened this issue 3 years ago • 5 comments

Description

Setting the ItemsSource property of a CollectionView to null while there is a selected item causes an exception to be thrown. This also happens if ItemsSource is set to null indirectly, in my case I found out while trying to set the Page.BindingContext to null.

Steps to Reproduce

  1. Have a collection view.
  2. Select an item.
  3. Set ItemsSource to null.

Expected Behavior

The collection view is empty without an error.

Actual Behavior

System.IndexOutOfRangeException IItemsViewSource is empty

Basic Information

  • Version with issue: 5.0.0.2291

Reproduction Link

https://github.com/GalaxiaGuy/xf-bugs/tree/bug/collectionview-null-binding

GalaxiaGuy avatar Dec 16 '21 15:12 GalaxiaGuy

The crash ultimately happens when the UICollectionViewController calls GetIndexForItem on the IItemsViewSource.

ListSource, for example searches for the item and returns NSIndexPath.Create(-1, -1) if it is not found (which is what happens when you change the ItemsSource to something not null and the selected item is not in the new source).

EmptySource however throws IndexOutOfRangeException in this situation (actually, it throws unconditionally).

It seems the easiest solution is to make EmptySource return NSIndexPath.Create(-1, -1) instead. This would match the behavior of most IndexOf methods, but I don't know if there are wider considerations.

GalaxiaGuy avatar Dec 17 '21 08:12 GalaxiaGuy

I am having this same issue - any workarounds identified?

daniel-smith-sonepar avatar Jul 26 '23 14:07 daniel-smith-sonepar

I was having the same problem on ios. Setting the selected item to null before setting the ItemSource to null solved the problem for me.

FM1973 avatar Jul 31 '23 06:07 FM1973

I am experiencing the same issue. The only difference is that I am using a library cold Prism. Whenever you dismiss the screen, the Prism library "Destroys" the page by setting its BindingContext to null. Unfortunately, when this happens and there's a SelectedItem on the CollectionView the above-mentioned exception is raised. Thankfully, for me the workaround was setting the SelectedItem to null before that happens and luckily Prism has IDestructible.Destroy interface where you can do "cleanup", in this case it was setting the SelectedItem to null

kyurkchyan avatar Sep 07 '23 16:09 kyurkchyan

I am experiencing the same issue. The only difference is that I am using a library cold Prism. Whenever you dismiss the screen, the Prism library "Destroys" the page by setting its BindingContext to null. Unfortunately, when this happens and there's a SelectedItem on the CollectionView the above-mentioned exception is raised. Thankfully, for me the workaround was setting the SelectedItem to null before that happens and luckily Prism has IDestructible.Destroy interface where you can do "cleanup", in this case it was setting the SelectedItem to null

Not using Prism, but FreshMVVM. Same issue when the page is dismissed. I wish there was a solution that didn't involve nulling this out every time.

lloydkevin avatar Nov 30 '23 21:11 lloydkevin