Skip to content

Fix CollectionView crash when setting SelectedItem then ItemsSource to null on iOS/MacCatalyst#30390

Closed
Copilot wants to merge 3 commits intomainfrom
copilot/fix-29937
Closed

Fix CollectionView crash when setting SelectedItem then ItemsSource to null on iOS/MacCatalyst#30390
Copilot wants to merge 3 commits intomainfrom
copilot/fix-29937

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Jul 2, 2025

Note

Are you waiting for the changes in this PR to be merged?
It would be very helpful if you could test the resulting artifacts from this PR and let us know in a comment if this change resolves your issue. Thank you!

Description

This PR fixes a crash that occurs on iOS and MacCatalyst when programmatically setting the SelectedItem property of a CollectionView and then immediately setting the ItemsSource property to null.

Problem

The issue was caused by a race condition in the SelectItem method of SelectableItemsViewController:

  1. The method checks if ItemsSource is null at the beginning
  2. It uses PerformBatchUpdates with a callback to select the item
  3. Between the initial null check and the callback execution, ItemsSource can be set to null
  4. The callback executes without re-checking, causing a crash when trying to select an item that no longer exists
// This scenario would crash before the fix:
collectionView.SelectedItem = items.FirstOrDefault(); // Set selected item
items = null; // Immediately set ItemsSource to null - causes crash

Solution

Added a null check inside the PerformBatchUpdates callback to ensure we don't attempt to select an item when the ItemsSource is null:

CollectionView.PerformBatchUpdates(null, _ =>
{
    // Re-check if ItemsSource is still valid before attempting to select the item
    if (ItemsView?.ItemsSource is null)
    {
        return;
    }
    CollectionView.SelectItem(index, true, UICollectionViewScrollPosition.None);
});

Testing

  • Created test case Issue29937 that reproduces the crash scenario
  • Verified the fix prevents the crash without affecting normal operation
  • Test validates that setting SelectedItem then immediately setting ItemsSource to null doesn't crash

Impact

  • Minimal Change: Only 4 lines added to prevent the race condition
  • Safe: No side effects - if ItemsSource is null, there's nothing to select anyway
  • Consistent: Follows existing null-check patterns used in other methods like ClearSelection and UpdatePlatformSelection
  • Targeted: Only affects the specific crash scenario without changing normal behavior

Fixes #29937.


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

…o null

Co-authored-by: PureWeen <5375137+PureWeen@users.noreply.github.com>
Copilot AI changed the title [WIP] [iOS/MacCatalyst] Setting SelectedItem Programmatically and Then Immediately Setting ItemsSource to Null Causes a Crash Fix CollectionView crash when setting SelectedItem then ItemsSource to null on iOS/MacCatalyst Jul 2, 2025
Copilot AI requested a review from PureWeen July 2, 2025 17:01
@PureWeen
Copy link
Copy Markdown
Member

PureWeen commented Jul 2, 2025

use your copilot instructions on reviewing already open PRs

And review this other PR to fix the same issue
#29940

Modify this PR based on what you learn from that PR

…dling

Co-authored-by: PureWeen <5375137+PureWeen@users.noreply.github.com>
Copy link
Copy Markdown
Contributor Author

Copilot AI commented Jul 2, 2025

use your copilot instructions on reviewing already open PRs

And review this other PR to fix the same issue
...

I've reviewed PR #29940 and adopted their comprehensive solution instead of my simple null check approach. Their fix is significantly more robust, handling multiple race conditions including reference changes, disposal states, and collection modifications. Applied the same pattern to both SelectableItemsViewController files and updated tests accordingly. Changes committed in 31dafb7.

@PureWeen PureWeen closed this Jul 2, 2025
@github-actions github-actions bot locked and limited conversation to collaborators Aug 2, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[iOS/MacCatalyst] Setting SelectedItem Programmatically and Then Immediately Setting ItemsSource to Null Causes a Crash

2 participants