Skip to content
This repository has been archived by the owner on May 1, 2024. It is now read-only.

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

Open
GalaxiaGuy opened this issue Dec 16, 2021 · 5 comments
Labels
s/unverified New report that has yet to be verified t/bug 🐛

Comments

@GalaxiaGuy
Copy link
Contributor

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 GalaxiaGuy added s/unverified New report that has yet to be verified t/bug 🐛 labels Dec 16, 2021
@GalaxiaGuy
Copy link
Contributor Author

GalaxiaGuy commented Dec 17, 2021

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.

@daniel-smith-sonepar
Copy link

I am having this same issue - any workarounds identified?

@FM1973
Copy link

FM1973 commented Jul 31, 2023

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.

@kyurkchyan
Copy link

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

@lloydkevin
Copy link

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.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
s/unverified New report that has yet to be verified t/bug 🐛
Projects
None yet
Development

No branches or pull requests

5 participants