Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for CollectionView with RefreshView when ObservableCollection<T>.Clear() throws System.ArgumentOutOfRangeException #26573

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

SuthiYuvaraj
Copy link
Contributor

@SuthiYuvaraj SuthiYuvaraj commented Dec 12, 2024

Root Cause:

The issue arises because the UICollectionView's internal _itemSource becomes empty during pull-to-refresh, even though the ItemsSource in the controller still holds a valid count. This discrepancy causes an invalid state, where the GetSizeForItem method tries to access an index that no longer exists, resulting in a System.ArgumentOutOfRangeException. The issue was further compounded by the fact that GetSizeForItem was being called before the CollectionChanged event was triggered. This occurs because the ContentOffset is set during the pull-to-refresh operation, which prematurely triggers layout recalculations, leading to an invalid state.

Description of Change:

The change ensures the ElementAt method safely accesses elements by validating the index with (index >= 0 && index < list.Count). Using a ternary operator, it immediately returns the item or -1 for invalid indices, preventing System.ArgumentOutOfRangeException and simplifying the logic for better reliability.

Issues Fixed

Fixes #23868

Tested the behaviour in the following platforms

  • Android
  • Windows
  • iOS
  • Mac

###Output Video

Before Issue Fix After Issue Fix
withoutfix.mov
withfix.mov

@dotnet-policy-service dotnet-policy-service bot added the community ✨ Community Contribution label Dec 12, 2024
Copy link
Contributor

Hey there @SuthiYuvaraj! Thank you so much for your PR! Someone from the team will get assigned to your PR shortly and we'll get it reviewed.

@jfversluis jfversluis added the area-controls-collectionview CollectionView, CarouselView, IndicatorView label Dec 12, 2024
@sheiksyedm sheiksyedm added the partner/syncfusion Issues / PR's with Syncfusion collaboration label Dec 12, 2024
@SuthiYuvaraj SuthiYuvaraj marked this pull request as ready for review December 12, 2024 13:31
Copy link
Member

@PureWeen PureWeen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add the following test to this PR?

public class Issue7678Ios : TestContentPage

When you copy the test over can you make sure to put it inside the XFIssue folder?

image

It looks like the ObservableItemSource changes were made in the following PR xamarin/Xamarin.Forms#7711 and I worry that the fixes here are reverting those fixes a bit.

@PureWeen PureWeen added this to the .NET 9 SR3 milestone Dec 12, 2024
@praveenkumarkarunanithi
Copy link
Contributor

praveenkumarkarunanithi commented Dec 13, 2024

Can you add the following test to this PR?

public class Issue7678Ios : TestContentPage

When you copy the test over can you make sure to put it inside the XFIssue folder?

image

It looks like the ObservableItemSource changes were made in the following PR xamarin/Xamarin.Forms#7711 and I worry that the fixes here are reverting those fixes a bit.

@PureWeen Regarding the changes to ObservableItemSource, we’d like to clarify that our fix now ensures functionality without modifying the Count property directly. Instead, we’ve implemented proper index validation to prevent exceptions when the list is empty or the index is out of range. This approach preserves the integrity of the existing logic and aligns with the previous changes introduced in PR xamarin/Xamarin.Forms#7711.
Additionally, we ensured the Issue7678 test case works as expected with the fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-controls-collectionview CollectionView, CarouselView, IndicatorView community ✨ Community Contribution partner/syncfusion Issues / PR's with Syncfusion collaboration
Projects
Status: Changes Requested
Development

Successfully merging this pull request may close these issues.

ObservableCollection<T>.Clear() throws System.ArgumentOutOfRangeException
5 participants