dotnet / maui

.NET MAUI is the .NET Multi-platform App UI, a framework for building native device applications spanning mobile, tablet, and desktop.
https://dot.net/maui
MIT License
22.06k stars 1.73k forks source link

CollectionView with Grouping Generates UICollectionView "attempting to apply nil layout attributes to view" on iOS #15862

Open fischberg opened 1 year ago

fischberg commented 1 year ago

Description

If I use a CollectionView with IsGrouped="true", when rendering the CollectionView, the app generates the following error message twice for each Group.

[Assert] UICollectionView internal inconsistency: attempting to apply nil layout attributes to view. View: <Microsoft_Maui_Controls_Handlers_Items_VerticalSupplementaryView: 0x7fd35db14c60; baseClass = UICollectionViewCell; frame = (0 0; 0 0); layer = <CALayer: 0x600000941b00>>; current attributes from view: (null); collection view: <UICollectionView: 0x7fd3662b8800; frame = (0 0; 374 896); clipsToBounds = YES; autoresize = W+H; gestureRecognizers = <NSArray: 0x600000d83bd0>; backgroundColor = UIExtendedGrayColorSpace 0 0; layer = <CALayer: 0x60000098f940>; contentOffset: {0, 0}; contentSize: {0, 0}; adjustedContentInset: {0, 0, 0, 0}; layout: <Microsoft_Maui_Controls_Handlers_Items_ListViewLayout: 0x7fd35e12cec0>; dataSource: <Microsoft_Maui_Controls_Handlers_Items_ReorderableItemsViewController_1: 0x7fd35daaf2d0>>

When I deploy a Release or Debug build to my physical iPhone, I can render the offending page 4 times. On the 5th attempt it crashes. I've been unable to determine the exact cause of the crash because no crash report is generated. These error messages are generated on the 1st 4 renderings and appear on the 5th attempt just before the app crashes. Regardless of whether or not these error messages are related to cause of my app crashing, the root cause should be resolved.

These error messages are generated when using Release and Debug builds and on both a physical device and simulator. However, the app only crashes on a physical device. Again, I'm not sure if this is the cause of the crash.

The error message I pasted above is generated from the repro code that I provided in this report. The error messages for my app are very similar. Below, I pasted the relevant output from the repro, not my actual app.

Steps to Reproduce

Build the CollectionViewDemos app from the provided repository. This is an exact copy from the Maui Samples GitHub repository. Start the app Select the "Grouping, Vertical list with DataTemplates" menu item. Observe the error messages in the Application Output.

Link to public reproduction project repository

https://github.com/fischberg/CollectionViewDemos.git

Version with bug

7.0.86

Last version that worked well

Unknown/Other

Affected platforms

I was not able test on other platforms

Affected platform versions

iOS 16.4

Did you find any workaround?

None.

Relevant log output

2023-06-26 15:10:14.406544-0500 CollectionViewDemos[31024:564650] [Assert] UICollectionView internal inconsistency: attempting to apply nil layout attributes to view. View: <Microsoft_Maui_Controls_Handlers_Items_VerticalSupplementaryView: 0x7fd2d4a426d0; baseClass = UICollectionViewCell; frame = (0 0; 0 0); layer = <CALayer: 0x6000024707a0>>; current attributes from view: (null); collection view: <UICollectionView: 0x7fd2dd428200; frame = (0 0; 374 896); clipsToBounds = YES; autoresize = W+H; gestureRecognizers = <NSArray: 0x6000020579f0>; backgroundColor = UIExtendedGrayColorSpace 0 0; layer = <CALayer: 0x6000024bf9c0>; contentOffset: {0, 0}; contentSize: {0, 0}; adjustedContentInset: {0, 0, 0, 0}; layout: <Microsoft_Maui_Controls_Handlers_Items_ListViewLayout: 0x7fd2d4bc9f40>; dataSource: <Microsoft_Maui_Controls_Handlers_Items_ReorderableItemsViewController_1: 0x7fd2d4bbdf40>>
2023-06-26 15:10:14.419870-0500 CollectionViewDemos[31024:564650] [Assert] UICollectionView internal inconsistency: attempting to apply nil layout attributes to view. View: <Microsoft_Maui_Controls_Handlers_Items_VerticalSupplementaryView: 0x7fd2d4bcde40; baseClass = UICollectionViewCell; frame = (0 0; 0 0); layer = <CALayer: 0x600002442c00>>; current attributes from view: (null); collection view: <UICollectionView: 0x7fd2dd428200; frame = (0 0; 374 896); clipsToBounds = YES; autoresize = W+H; gestureRecognizers = <NSArray: 0x6000020579f0>; backgroundColor = UIExtendedGrayColorSpace 0 0; layer = <CALayer: 0x6000024bf9c0>; contentOffset: {0, 0}; contentSize: {0, 0}; adjustedContentInset: {0, 0, 0, 0}; layout: <Microsoft_Maui_Controls_Handlers_Items_ListViewLayout: 0x7fd2d4bc9f40>; dataSource: <Microsoft_Maui_Controls_Handlers_Items_ReorderableItemsViewController_1: 0x7fd2d4bbdf40>>
2023-06-26 15:10:14.434058-0500 CollectionViewDemos[31024:564650] [Assert] UICollectionView internal inconsistency: attempting to apply nil layout attributes to view. View: <Microsoft_Maui_Controls_Handlers_Items_VerticalSupplementaryView: 0x7fd2d4a44ad0; baseClass = UICollectionViewCell; frame = (0 0; 0 0); layer = <CALayer: 0x600002470b60>>; current attributes from view: (null); collection view: <UICollectionView: 0x7fd2dd428200; frame = (0 0; 374 896); clipsToBounds = YES; autoresize = W+H; gestureRecognizers = <NSArray: 0x6000020579f0>; backgroundColor = UIExtendedGrayColorSpace 0 0; layer = <CALayer: 0x6000024bf9c0>; contentOffset: {0, 0}; contentSize: {0, 0}; adjustedContentInset: {0, 0, 0, 0}; layout: <Microsoft_Maui_Controls_Handlers_Items_ListViewLayout: 0x7fd2d4bc9f40>; dataSource: <Microsoft_Maui_Controls_Handlers_Items_ReorderableItemsViewController_1: 0x7fd2d4bbdf40>>
2023-06-26 15:10:14.444588-0500 CollectionViewDemos[31024:564650] [Assert] UICollectionView internal inconsistency: attempting to apply nil layout attributes to view. View: <Microsoft_Maui_Controls_Handlers_Items_VerticalSupplementaryView: 0x7fd2d4a46a50; baseClass = UICollectionViewCell; frame = (0 0; 0 0); layer = <CALayer: 0x6000024710e0>>; current attributes from view: (null); collection view: <UICollectionView: 0x7fd2dd428200; frame = (0 0; 374 896); clipsToBounds = YES; autoresize = W+H; gestureRecognizers = <NSArray: 0x6000020579f0>; backgroundColor = UIExtendedGrayColorSpace 0 0; layer = <CALayer: 0x6000024bf9c0>; contentOffset: {0, 0}; contentSize: {0, 0}; adjustedContentInset: {0, 0, 0, 0}; layout: <Microsoft_Maui_Controls_Handlers_Items_ListViewLayout: 0x7fd2d4bc9f40>; dataSource: <Microsoft_Maui_Controls_Handlers_Items_ReorderableItemsViewController_1: 0x7fd2d4bbdf40>>
2023-06-26 15:10:14.459835-0500 CollectionViewDemos[31024:564650] [Assert] UICollectionView internal inconsistency: attempting to apply nil layout attributes to view. View: <Microsoft_Maui_Controls_Handlers_Items_VerticalSupplementaryView: 0x7fd2d4a479d0; baseClass = UICollectionViewCell; frame = (0 0; 0 0); layer = <CALayer: 0x600002471620>>; current attributes from view: (null); collection view: <UICollectionView: 0x7fd2dd428200; frame = (0 0; 374 896); clipsToBounds = YES; autoresize = W+H; gestureRecognizers = <NSArray: 0x6000020579f0>; backgroundColor = UIExtendedGrayColorSpace 0 0; layer = <CALayer: 0x6000024bf9c0>; contentOffset: {0, 0}; contentSize: {0, 0}; adjustedContentInset: {0, 0, 0, 0}; layout: <Microsoft_Maui_Controls_Handlers_Items_ListViewLayout: 0x7fd2d4bc9f40>; dataSource: <Microsoft_Maui_Controls_Handlers_Items_ReorderableItemsViewController_1: 0x7fd2d4bbdf40>>
2023-06-26 15:10:14.472032-0500 CollectionViewDemos[31024:564650] [Assert] UICollectionView internal inconsistency: attempting to apply nil layout attributes to view. View: <Microsoft_Maui_Controls_Handlers_Items_VerticalSupplementaryView: 0x7fd2d4a49950; baseClass = UICollectionViewCell; frame = (0 0; 0 0); layer = <CALayer: 0x600002471760>>; current attributes from view: (null); collection view: <UICollectionView: 0x7fd2dd428200; frame = (0 0; 374 896); clipsToBounds = YES; autoresize = W+H; gestureRecognizers = <NSArray: 0x6000020579f0>; backgroundColor = UIExtendedGrayColorSpace 0 0; layer = <CALayer: 0x6000024bf9c0>; contentOffset: {0, 0}; contentSize: {0, 0}; adjustedContentInset: {0, 0, 0, 0}; layout: <Microsoft_Maui_Controls_Handlers_Items_ListViewLayout: 0x7fd2d4bc9f40>; dataSource: <Microsoft_Maui_Controls_Handlers_Items_ReorderableItemsViewController_1: 0x7fd2d4bbdf40>>
2023-06-26 15:10:14.485186-0500 CollectionViewDemos[31024:564650] [Assert] UICollectionView internal inconsistency: attempting to apply nil layout attributes to view. View: <Microsoft_Maui_Controls_Handlers_Items_VerticalSupplementaryView: 0x7fd2d3f2b090; baseClass = UICollectionViewCell; frame = (0 0; 0 0); layer = <CALayer: 0x6000024776a0>>; current attributes from view: (null); collection view: <UICollectionView: 0x7fd2dd428200; frame = (0 0; 374 896); clipsToBounds = YES; autoresize = W+H; gestureRecognizers = <NSArray: 0x6000020579f0>; backgroundColor = UIExtendedGrayColorSpace 0 0; layer = <CALayer: 0x6000024bf9c0>; contentOffset: {0, 0}; contentSize: {0, 0}; adjustedContentInset: {0, 0, 0, 0}; layout: <Microsoft_Maui_Controls_Handlers_Items_ListViewLayout: 0x7fd2d4bc9f40>; dataSource: <Microsoft_Maui_Controls_Handlers_Items_ReorderableItemsViewController_1: 0x7fd2d4bbdf40>>
2023-06-26 15:10:14.495625-0500 CollectionViewDemos[31024:564650] [Assert] UICollectionView internal inconsistency: attempting to apply nil layout attributes to view. View: <Microsoft_Maui_Controls_Handlers_Items_VerticalSupplementaryView: 0x7fd2d4d17b90; baseClass = UICollectionViewCell; frame = (0 0; 0 0); layer = <CALayer: 0x6000024b27c0>>; current attributes from view: (null); collection view: <UICollectionView: 0x7fd2dd428200; frame = (0 0; 374 896); clipsToBounds = YES; autoresize = W+H; gestureRecognizers = <NSArray: 0x6000020579f0>; backgroundColor = UIExtendedGrayColorSpace 0 0; layer = <CALayer: 0x6000024bf9c0>; contentOffset: {0, 0}; contentSize: {0, 0}; adjustedContentInset: {0, 0, 0, 0}; layout: <Microsoft_Maui_Controls_Handlers_Items_ListViewLayout: 0x7fd2d4bc9f40>; dataSource: <Microsoft_Maui_Controls_Handlers_Items_ReorderableItemsViewController_1: 0x7fd2d4bbdf40>>
2023-06-26 15:10:14.510926-0500 CollectionViewDemos[31024:564650] [Assert] UICollectionView internal inconsistency: attempting to apply nil layout attributes to view. View: <Microsoft_Maui_Controls_Handlers_Items_VerticalSupplementaryView: 0x7fd2d4bd0110; baseClass = UICollectionViewCell; frame = (0 0; 0 0); layer = <CALayer: 0x600002442e20>>; current attributes from view: (null); collection view: <UICollectionView: 0x7fd2dd428200; frame = (0 0; 374 896); clipsToBounds = YES; autoresize = W+H; gestureRecognizers = <NSArray: 0x6000020579f0>; backgroundColor = UIExtendedGrayColorSpace 0 0; layer = <CALayer: 0x6000024bf9c0>; contentOffset: {0, 0}; contentSize: {0, 0}; adjustedContentInset: {0, 0, 0, 0}; layout: <Microsoft_Maui_Controls_Handlers_Items_ListViewLayout: 0x7fd2d4bc9f40>; dataSource: <Microsoft_Maui_Controls_Handlers_Items_ReorderableItemsViewController_1: 0x7fd2d4bbdf40>>
2023-06-26 15:10:14.523925-0500 CollectionViewDemos[31024:564650] [Assert] UICollectionView internal inconsistency: attempting to apply nil layout attributes to view. View: <Microsoft_Maui_Controls_Handlers_Items_VerticalSupplementaryView: 0x7fd2d3f2d1d0; baseClass = UICollectionViewCell; frame = (0 0; 0 0); layer = <CALayer: 0x6000024779c0>>; current attributes from view: (null); collection view: <UICollectionView: 0x7fd2dd428200; frame = (0 0; 374 896); clipsToBounds = YES; autoresize = W+H; gestureRecognizers = <NSArray: 0x6000020579f0>; backgroundColor = UIExtendedGrayColorSpace 0 0; layer = <CALayer: 0x6000024bf9c0>; contentOffset: {0, 0}; contentSize: {0, 0}; adjustedContentInset: {0, 0, 0, 0}; layout: <Microsoft_Maui_Controls_Handlers_Items_ListViewLayout: 0x7fd2d4bc9f40>; dataSource: <Microsoft_Maui_Controls_Handlers_Items_ReorderableItemsViewController_1: 0x7fd2d4bbdf40>>
ToolmakerSteve commented 1 year ago

FYI: The corresponding StackOverflow question is https://stackoverflow.com/questions/76560978/how-to-find-cause-of-app-crashing-on-ios-device-but-not-on-simulator-no-crash.

ghost commented 1 year ago

Hi @fischberg. We have added the "s/try-latest-version" label to this issue, which indicates that we'd like you to try and reproduce this issue on the latest available public version. This can happen because we think that this issue was fixed in a version that has just been released, or the information provided by you indicates that you might be working with an older version.

You can install the latest version by installing the latest Visual Studio (Preview) with the .NET MAUI workload installed. If the issue still persists, please let us know with any additional details and ideally a reproduction project provided through a GitHub repository.

This issue will be closed automatically in 7 days if we do not hear back from you by then - please feel free to re-open it if you come back to this issue after that time.

fischberg commented 1 year ago

@PureWeen I'm running VS Studio for Mac. If I go to updates, it shows no updates available. On your release page, it shows that VS 17.6 build 1569 is the latest release which is what I'm running. Are you suggesting I manually install the latest preview version of .NET 8?

fischberg commented 1 year ago

I installed .NET SDK version 8 preview 5 and installed the MAUI workload. I get the same error messages and the same crash behavior. Unfortunately, no change.

ghost commented 1 year ago

We've added this issue to our backlog, and we will work to address it as time and resources allow. If you have any additional information or questions about this issue, please leave a comment. For additional info about issue management, please read our Triage Process.

fischberg commented 1 year ago

I have determined definitively that those error messages are not related to the cause of my app crashing.

XamlTest commented 1 year ago

Verified this on Visual Studio Enterprise 17.7.0 Preview 5.0. Repro on iOS 16.4 .NET 8, not repro on Android 13.0-API33 and Windows 11 with below Project: CollectionViewDemos.zip

kyurkchyan commented 11 months ago

Having the same issue. With NET 8 RC2

wsb9 commented 11 months ago

TL; DR

This is API misuse. In the UICollectionViewDataSource.collectionView(_ collection: UICollectionView, cellForItemAt indexPath: IndexPath) method implementation, the index path you pass to collection.dequeueReusableCell when getting the cell should be exactly the same as index path you received in indexPath parameter.

Details

I've hit exactly the same issue developing natively for iOS. Since this thread is the only relevant result on entire google, decided to post possible solution here for future seekers.

In complex, compound data sources containing separate section sources, it's easy to miss proper section value in index path, and ask collection view to return cell for path that doesn't exist in reality.

When user scrolls, collection view first uses its UICollectionViewLayout instance to determine what cells are going to appear next, and then asks its data source for these cells. When you call dequeueReusableCell in your data source implementation, collection view prepares the cell and assigns cached layout attributes to it before returning the cell to you (for optimization purposes).

However, when you pass index path that is wrong (for example, when you have multiple sections with different item counts), collection layout could not figure out proper attributes for these cells and issues tons of warnings.

Here's the possible cause in project sources: section 0 is always used when fetching a view. https://github.com/dotnet/maui/blob/71687020392deca465f051bc808906836068cc39/src/Controls/src/Core/Handlers/Items/iOS/GroupableItemsViewController.cs#L184

Impact

In grouped collections where all groups contains same items: no logic impacts, small extra autolayout overhead. Serious scrolling slowdown when debugger is attached to the app (due to synchronous nature of xcode log reader). No performance drop when app runs on its own.

In collections where groups contains different cell types, especially for sections with orthogonal scrolling behavior: visual artifacts, broken orthogonal scrolling. Even hit a collection view total hang (only once). Same performance penalty when debugger attached.

Conclusion

So, just make sure that you always pass valid index paths when asking for reusable cells.

Hope this helps.

ppereira-serviceonsites commented 9 months ago

Is there any thoughts on how to fix this so far? Thx @wsb9 for detailing the guts of the problem... BUT... not sure how to take this information and do anything with it other than to complain to Microsoft to fix it?

TrueMoniker commented 9 months ago

+1 This is occurring on an iOS simulator using 17.2. It seems to work on simulators using 16.4 Edit: It is not crashing on simulators using 16.4, however it is throwing the message in the application output, and the simulator slows down dramatically if it has to render several controls in the collection.

scriptBoris commented 5 months ago

Any news?

Skarzag commented 1 month ago

Any news on this big problem with grouped data in collectionview ?

LeoJHarris commented 3 weeks ago

Same issue, app works perfectly on Android but iOS crashes 😕 need a fix or work around badly, this is blocking.

fischberg commented 3 weeks ago

@LeoJHarris - while I still get these error messages on iOS, my app never crashes because of them. May want to verify that your crash isn’t due to something else.

LeoJHarris commented 3 weeks ago

@fischberg I will investigate this further, I was not having the issue Android, given that we have just completed out app migration from Xamarin, I have a few niggly issues that I probably need to diagnose further to understand if its a framework issue or not.