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

[Regression] CollectionView NSRangeException #11311

Closed
brminnick opened this issue Jul 4, 2020 · 1 comment
Closed

[Regression] CollectionView NSRangeException #11311

brminnick opened this issue Jul 4, 2020 · 1 comment
Assignees
Labels
4.6.0 regression on 4.6.0 a/collectionview excellent-report i/regression in-progress This issue has an associated pull request that may resolve it! t/bug 🐛
Milestone

Comments

@brminnick
Copy link
Contributor

Description

In Xamarin.Forms v4.7.0.1080 and v4.7.0.968, on iOS, an NSRangeException is thrown when a CollectionView.ItemSource is updated:

Objective-C exception thrown. Name: NSRangeException Reason: *** -[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array

Steps to Reproduce

  1. Download / clone this repo: https://github.com/brminnick/CollectionViewBoundsExceptionRepro
  2. In Visual Studio, open CollectionViewBoundsExceptionRepro.sln
  3. In Visual Studio, buld/deploy CollectionViewBoundsExceptionRepro.iOS to an iOS Simulator or Device
  4. Confirm NSRangeException is thrown on launch

Expected Behavior

CollectionView.ItemSource should update without throwing an Exception

Actual Behavior

CollectionView.ItemSource throws NSRangeException

Basic Information

  • Version with issue: Confirmed on v4.7.0.1080 and v4.7.0.968
  • Last known good version: v4.7.0.937-pre4
  • Platform Target Frameworks:
    • iOS: 13.20.1.18

Screenshots

Reproduction Link

https://github.com/brminnick/CollectionViewBoundsExceptionRepro

Work Arounds

Option #1. Downgrade Xamarin.Forms

Downgrade to Xamarin.Forms v4.7.0.937-pre4

Option #2. Remove CollectionView.Footer

Set CollectionView.Footer = null

  1. In CollectionViewPage.cs, change Footer = new BoxView { BackgroundColor = Color.Red, HeightRequest = 53 } to Footer = null

https://github.com/brminnick/CollectionViewBoundsExceptionRepro/blob/8a6636d7af57f289113e800cfa0d0160cad78d6d/Src/CollectionViewBoundsExceptionRepro/CollectionViewPage.cs#L17

Option #3. Replace IEnumerable<int> with ObservableCollection<int>

  1. In CollectionViewModel.cs, remove public IEnumerable<int> ScoreCollectionList
  2. In CollectionViewModel.cs, remove IEnumerable<int>? _scoreCollectionList;
  3. In CollectionViewModel.cs, add public ObservableCollection<int> ScoreCollectionList { get; } = new ObservableCollection<int>()
  4. In CollectionViewModel.cs, in ExecuteRefreshCommand refactor logic for ObservableCollection :

Replace this code:

void ExecuteRefreshCommand()
{
    ScoreCollectionList = Enumerable.Range(0, 100).ToList();
    IsRefreshing = false;
}

with this code:

void ExecuteRefreshCommand()
{
    foreach (var number in Enumerable.Range(0, 100))
        ScoreCollectionList.Add(number);

    IsRefreshing = false;
}
@brminnick brminnick added s/unverified New report that has yet to be verified t/bug 🐛 labels Jul 4, 2020
@PureWeen PureWeen added 4.6.0 regression on 4.6.0 and removed s/unverified New report that has yet to be verified labels Jul 9, 2020
@hartez hartez self-assigned this Jul 9, 2020
hartez added a commit that referenced this issue Jul 10, 2020
@hartez hartez added the in-progress This issue has an associated pull request that may resolve it! label Jul 10, 2020
hartez added a commit that referenced this issue Jul 23, 2020
@samhouts samhouts added this to the 5.0.0 milestone Aug 13, 2020
@mchlkoval
Copy link

I am on older xamarin version (can't just update it for now, version 4.3 iirc) and I too run into this error; however for me it is part of a ViewCell used in a data template selector; it looks like it holds onto an old reference of a view cell, and when we try to select a list view with multiple items, compared to when a previous one we just interacted with has less items we get that exception thrown.

@samhouts samhouts modified the milestones: 5.0.0, 4.8.0 Aug 31, 2020
hartez added a commit that referenced this issue Sep 8, 2020
hartez added a commit that referenced this issue Sep 9, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
4.6.0 regression on 4.6.0 a/collectionview excellent-report i/regression in-progress This issue has an associated pull request that may resolve it! t/bug 🐛
Projects
None yet
Development

No branches or pull requests

6 participants