maui: [iOS] Crash during Render after adding items to an ObservableCollection

Description

I get the following crash after adding items to an ObservableCollection for the NewsReader app.

  • The crash is only seen on iOS.
  • Android and Windows works fine.
  • Also the Xamarin Forms 5 version of the same app works fine on iOS.

Related: https://github.com/dotnet/maui/issues/4756 (Maybe this commit did not solve all cases: https://github.com/dotnet/maui/commit/cf2d2cd075365ab902d7998d75259f34c5c75853)

Crash:

ObjCRuntime.ObjCException: 'Objective-C exception thrown.  Name: NSInternalInconsistencyException Reason: 
Invalid update: invalid number of sections. The number of sections contained in the collection view after the update (6) 
must be equal to the number of sections contained in the collection view before the update (6), 
plus or minus the number of sections inserted or deleted (1 inserted, 0 deleted). 
Collection view: <UICollectionView: 0x10acdea00; frame = (0 0; 828 1540); clipsToBounds = YES; autoresize = W+H; 
gestureRecognizers = <NSArray: 0x283989b00>; layer = <CALayer: 0x2833156e0>; contentOffset: {0, 0}; 
contentSize: {828, 708}; adjustedContentInset: {0, 0, 0, 0}; 
layout: <Microsoft_Maui_Controls_Handlers_Items_ListViewLayout: 0x1014af780>; 
dataSource: <Microsoft_Maui_Controls_Handlers_Items_ReorderableItemsViewController_1: 0x1014aace0>>
Native stack trace:
	0   CoreFoundation                      0x00000001ac71129c 6B22DD81-3585-3BE6-BC77-BA19810EC0F2 + 627356
	1   libobjc.A.dylib                     0x00000001c5441744 objc_exception_throw + 60
	2   Foundation                          0x00000001adfa2340 EE1ABAF2-3D71-37FB-9067-15AA79528619 + 1254208
	3   UIKitCore                           0x00000001aeee0090 137A95AA-DA6D-332C-BC01-E13BB9B6E317 + 4104336
	4   UIKitCore                           0x00000001af136600 137A95AA-DA6D-332C-BC01-E13BB9B6E317 + 6555136
	5   UIKitCore                           0x00000001af077f0c 137A95AA-DA6D-332C-BC01-E13BB9B6E317 + 5775116
	6   NewsReader.Presentation             0x0000000100a0a5d8 _ZN16XamarinCallState3selEv + 3468
	7   NewsReader.Presentation             0x00000001009dd32c NewsReader.Presentation + 86828
	8   NewsReader.Presentation             0x0000000100ced178 _ZNK3icu6number23NumberFormatterSettingsINS0_24LocalizedNumberFormatterEE10toSkeletonER10UErrorCode + 2149724
	9   NewsReader.Presentation             0x0000000100ce2764 _ZNK3icu6number23NumberFormatterSettingsINS0_24LocalizedNumberFormatterEE10toSkeletonER10UErrorCode + 2106184
	10  NewsReader.Presentation             0x0000000100ce0b68 _ZNK3icu6number23NumberFormatterSettingsINS0_24LocalizedNumberFormatterEE10toSkeletonER10UErrorCode + 2099020
	11  NewsReader.Presentation             0x0000000100caefb0 _ZNK3icu6number23NumberFormatterSettingsINS0_24LocalizedNumberFormatterEE10toSkeletonER10UErrorCode + 1895316
	12  NewsReader.Presentation             0x0000000100bf949c _ZNK3icu6number23NumberFormatterSettingsINS0_24LocalizedNumberFormatterEE10toSkeletonER10UErrorCode + 1151104
	13  NewsReader.Presentation             0x0000000100bfce48 _ZNK3icu6number23NumberFormatterSettingsINS0_24LocalizedNumberFormatterEE10toSkeletonER10UErrorCode + 1165868
	14  NewsReader.Presentation             0x0000000100d26808 _ZNK3icu6number23NumberFormatterSettingsINS0_24LocalizedNumberFormatterEE10toSkeletonER10UErrorCode + 2384876
	15  NewsReader.Presentation             0x0000000100d4b690 _ZNK3icu6number23NumberFormatterSettingsINS0_24LocalizedNumberFormatterEE10toSkeletonER10UErrorCode + 2536052
	16  Foundation                          0x00000001adeadf00 EE1ABAF2-3D71-37FB-9067-15AA79528619 + 253696
	17  CoreFoundation                      0x00000001ac733414 6B22DD81-3585-3BE6-BC77-BA19810EC0F2 + 766996
	18  CoreFoundation                      0x00000001ac7441a0 6B22DD81-3585-3BE6-BC77-BA19810EC0F2 + 836000
	19  CoreFoundation                      0x00000001ac67d694 6B22DD81-3585-3BE6-BC77-BA19810EC0F2 + 22164
	20  CoreFoundation                      0x00000001ac68305c 6B22DD81-3585-3BE6-BC77-BA19810EC0F2 + 45148
	21  CoreFoundation                      0x00000001ac696bc8 CFRunLoopRunSpecific + 600
	22  GraphicsServices                    0x00000001c8802374 GSEventRunModal + 164
	23  UIKitCore                           0x00000001af00ab58 137A95AA-DA6D-332C-BC01-E13BB9B6E317 + 5327704
	24  UIKitCore                           0x00000001aed8c090 UIApplicationMain + 364
	25  NewsReader.Presentation             0x0000000100cedc58 _ZNK3icu6number23NumberFormatterSettingsINS0_24LocalizedNumberFormatterEE10toSkeletonER10UErrorCode + 2152508
	26  NewsReader.Presentation             0x0000000100cece60 _ZNK3icu6number23NumberFormatterSettingsINS0_24LocalizedNumberFormatterEE10toSkeletonER10UErrorCode + 2148932
	27  NewsReader.Presentation             0x0000000100ce2668 _ZNK3icu6number23NumberFormatterSettingsINS0_24LocalizedNumberFormatterEE10toSkeletonER10UErrorCode + 2105932
	28  NewsReader.Presentation             0x0000000100ce0b68 _ZNK3icu6number23NumberFormatterSettingsINS0_24LocalizedNumberFormatterEE10toSkeletonER10UErrorCode + 2099020
	29  NewsReader.Presentation             0x0000000100caefb0 _ZNK3icu6number23NumberFormatterSettingsINS0_24LocalizedNumberFormatterEE10toSkeletonER10UErrorCode + 1895316
	30  NewsReader.Presentation             0x0000000100bf949c _ZNK3icu6number23NumberFormatterSettingsINS0_24LocalizedNumberFormatterEE10toSkeletonER10UErrorCode + 1151104
	31  NewsReader.Presentation             0x0000000100bfef38 _ZNK3icu6number23NumberFormatterSettingsINS0_24LocalizedNumberFormatterEE10toSkeletonER10UErrorCode + 1174300
	32  NewsReader.Presentation             0x0000000100cb40c8 _ZNK3icu6number23NumberFormatterSettingsINS0_24LocalizedNumberFormatterEE10toSkeletonER10UErrorCode + 1916076
	33  NewsReader.Presentation             0x0000000100a0963c xamarin_log + 21332
	34  NewsReader.Presentation             0x0000000100d21ec4 _ZNK3icu6number23NumberFormatterSettingsINS0_24LocalizedNumberFormatterEE10toSkeletonER10UErrorCode + 2366120
	35  dyld                                0x0000000101175da4 start + 520
'

Steps to Reproduce

Link to public reproduction project repository

https://github.com/jbe2277/waf/tree/5a957034c1d92f45402c2069e180fecdc54873d0

Version with bug

6.0.486 (current)

Last version that worked well

Unknown/Other

Affected platforms

iOS

Affected platform versions

iOS 15

Did you find any workaround?

No

Relevant log output

No response

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 5
  • Comments: 22 (1 by maintainers)

Most upvoted comments

I’m also experiencing this error. In my testing it seems like the error only happens on iOS and specifically affects Collection Views with Grouped data when you clear the observable collection then add an item. You can see that it has to do with the collection view grouping in my test repository by toggling the CollectionView’s IsGrouped value.

Steps to Reproduce:

  1. Run Test App on iOS.
  2. Tap the Reset Collection button.
  3. If IsGrouped = true then the app will crash.

Neither invoking on main thread or adding a delay between the clear and add worked for me. But swapping the entire groups collection did. I just implemented it as a notifying property.

List<MyGroup> groups;
public List<MyGroup> Groups { get => groups; set => SetProperty(ref groups, value); }

Then replace the entire List with another one that contains the new groups.

It may also work if you manipulate your observable collection directly via the Items property and then manually fire the change event.

It seems this is a new exception in iOS 16.4, plenty of Swift devs are reporting the same problem.