Xamarin.Forms
Xamarin.Forms copied to clipboard
[Bug] [iOS] CollectionView crashes the Xamarin Forms App
Description
I have a Xamarin Forms app that has a collectionview that loads in items from a API as you scroll down in the collectionview. Sometimes when i scroll in the iOS app i get an error and the app crashes. This issue only occurs on iOS.
Steps to Reproduce
- Navigate to Page with collectionview
- Scroll down on the Page
- Error occur and app crashes
Expected Behavior
No crash just keep scrolling
Actual Behavior
Sometimes it crashes the app.
Basic Information
- Version with issue: 5.0.0.2401
- Platform Target Frameworks:
- iOS: 15.4
Environment
This is the error i get and it seems that it thinks that the itemssource in the collectionview is empty and then when it finds out its not empty and then crashes.
{System.NullReferenceException: Object reference not set to an instance of an object at Xamarin.Forms.Platform.iOS.ItemsViewController`1[TItemsView].CheckForEmptySource () [0x00028]
in D:\a\_work\1\s\Xamarin.Forms.Platform.iOS\CollectionView\ItemsViewController.cs:117
at Xamarin.Forms.Platform.iOS.ItemsViewController`1[TItemsView].NumberOfSections (UIKit.UICollectionView collectionView) [0x00000] in D:\a\_work\1\s\Xamarin.Forms.Platform.iOS\CollectionView\ItemsViewController.cs:256
at (wrapper managed-to-native) ObjCRuntime.Messaging.void_objc_msgSendSuper(intptr,intptr)
at UIKit.UICollectionViewLayout.PrepareLayout () [0x00023] in /Users/builder/azdo/_work/1/s/xamarin-macios/src/build/ios/native/UIKit/UICollectionViewLayout.g.cs:496
at Xamarin.Forms.Platform.iOS.ItemsViewLayout.PrepareLayout () [0x00000] in D:\a\_work\1\s\Xamarin.Forms.Platform.iOS\CollectionView\ItemsViewLayout.cs:415
at (wrapper managed-to-native) UIKit.UIApplication.UIApplicationMain(int,string[],intptr,intptr)
at UIKit.UIApplication.Main (System.String[] args, System.Type principalClass, System.Type delegateClass) [0x0003b] in /Users/builder/azdo/_work/1/s/xamarin-macios/src/UIKit/UIApplication.cs:85
at <ProjectName>.iOS.Application.Main (System.String[] args) [0x00001] in C:\Users\JesperEngdahl\source\repos\<ProjectName>\Main.cs:17 }
Yup I can confirm same issue here, glad to discover I'm not the only one but unfortunately the iOS issues keep coming 😢 I've only tested on iOS 15.0 as well.
@p3pp33ng are you able to provide a sample that replicates the issue? I tried to reproduce this issue with a stripped out version of the same app. If you provide a sample app that can reproduce this issue a fix will arrive sooner.
I've managed to reproduce this issue with the below sample and I run the app on an emulator using an iPhone 12 Pro 15.5


If you swipe the row in blue background and tap the remove the crash should occur
Hello @LeoJHarris, First and foremost very sorry for the late reply to your incredible contribution of building a sample to test with for this issue. I havent had the time to test it out, but will make it a priority first thing next week, hopefully this will help to find the underlaying cause of the shared problem we have.
Happy weekend and i will follow up this under next week.
The issue could be with the fact that you have SwipeView within Collectionview. I experienced many issues with this, which lead me to have separate layouts for iOS and Android. I utilized the CarouselView instead in a way that mimics the swipeview behavior.
I was facing a similar case in UWP, where the app crashed when I tried to programatically scroll the collection view right when it becomes visible. The issue resolved when I put the CollectionView inside a RefreshView
I have the same problem on iOS. Any ideas how for fix/workaround?
I have the same issue on iOS.
I have a CollectionView inside a RefreshView. The items template have a SwipeView. After many times refreshing the collection, it crashes with the same error. Removing the SwipeView from the items template doesn't help.
Same here.
The changes mentioned in the commit referenced above already exist now in Xamarin.Forms' repo.
Please try the latest release of Xamarin.Forms to see if it resolves your issue, if not, please provide a more up to date and smaller reproduction project of the issue.
@Redth yeah I noticed the PR after I started working on the fix. I believe this issue can be closed.
@Redth I can confirm I am not seeing this crash from upgrading from 5.0.0.2478 to 5.0.0.2622