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
22k stars 1.72k forks source link

[iOS] Crash during Render after adding items to an ObservableCollection #10163

Closed jbe2277 closed 1 year ago

jbe2277 commented 2 years ago

Description

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

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

ghost commented 2 years ago

We've moved this issue to the Backlog milestone. This means that it is not going to be worked on for the coming release. We will reassess the backlog following the current release and consider this item at that time. To learn more about our issue management process and to have better expectation regarding different types of issues you can read our Triage Process.

EthanHipps commented 1 year ago

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.
tsquillario commented 1 year ago

Experiencing the same issue just on iOS with a grouped CollectionView bound to an ObservableCollection.

rjspencer91 commented 1 year ago

+1 Also experiencing this

tranb3r commented 1 year ago

+1 Same issue for me on iOS (but no issue on Android)

Native stack trace:
    0   CoreFoundation                      0x00007ff8004278cb __exceptionPreprocess + 242
    1   libobjc.A.dylib                     0x00007ff80004dba3 objc_exception_throw + 48
    2   Foundation                          0x00007ff800b8637c _userInfoForFileAndLine + 0
    3   UIKitCore                           0x0000000116fcf525 -[UICollectionView _endItemAnimationsWithInvalidationContext:tentativelyForReordering:animator:collectionViewAnimator:] + 13198
    4   UIKitCore                           0x0000000116fcad2b -[UICollectionView _updateSections:updateAction:] + 402
    5   UIKitCore                           0x0000000116fcadf7 -[UICollectionView insertSections:] + 64
    6   libxamarin-dotnet-debug.dylib       0x0000000107fb1379 xamarin_dyn_objc_msgSend + 217
    7   ???                                 0x000000011dd66ca7 0x0 + 4795559079
    8   libmonosgen-2.0.dylib               0x000000010881a43a ves_pinvoke_method + 474
    9   libmonosgen-2.0.dylib               0x000000010880c6cc interp_exec_method + 3692
    10  libmonosgen-2.0.dylib               0x0000000108809e93 interp_runtime_invoke + 259
    11  libmonosgen-2.0.dylib               0x00000001086309ad mono_runtime_try_invoke + 157
    12  libmonosgen-2.0.dylib               0x0000000108632b3f mono_runtime_invoke + 95
    13  xxx.Maui                            0x0000000105798fe8 _ZL30native_to_managed_trampoline_9P11objc_objectP13objc_selectorPP11_MonoMethodj + 280
    14  xxx.Maui                            0x00000001057997e9 -[__MonoMac_NSAsyncActionDispatcher xamarinApplySelector] + 41
    15  Foundation                          0x00007ff800c7f5f7 __NSThreadPerformPerform + 177
    16  CoreFoundation                      0x00007ff800387035 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
    17  CoreFoundation                      0x00007ff800386f74 __CFRunLoopDoSource0 + 157
    18  CoreFoundation                      0x00007ff8003867d1 __CFRunLoopDoSources0 + 308
    19  CoreFoundation                      0x00007ff800380e73 __CFRunLoopRun + 927
    20  CoreFoundation                      0x00007ff8003806f7 CFRunLoopRunSpecific + 560
    21  GraphicsServices                    0x00007ff809c5c28a GSEventRunModal + 139
    22  UIKitCore                           0x0000000117aac62b -[UIApplication _run] + 994
    23  UIKitCore                           0x0000000117ab1547 UIApplicationMain + 123
    24  libxamarin-dotnet-debug.dylib       0x0000000107f6ba5a xamarin_UIApplicationMain + 58
    25  libmonosgen-2.0.dylib               0x000000010881b609 do_icall + 345
    26  libmonosgen-2.0.dylib               0x000000010881a113 do_icall_wrapper + 291
    27  libmonosgen-2.0.dylib               0x000000010880c52c interp_exec_method + 3276
    28  libmonosgen-2.0.dylib               0x0000000108809e93 interp_runtime_invoke + 259
    29  libmonosgen-2.0.dylib               0x000000010862eef8 mono_runtime_invoke_checked + 136
    30  libmonosgen-2.0.dylib               0x0000000108636f0c do_exec_main_checked + 92
    31  libmonosgen-2.0.dylib               0x0000000108765ec2 mono_jit_exec + 354
    32  libxamarin-dotnet-debug.dylib       0x0000000107fafe77 xamarin_main + 1927
    33  xxxx.Maui                           0x000000010582ca44 main + 52
    34  dyld                                0x00000001060782bf start_sim + 10
    35  ???                                 0x000000010675752e 0x0 + 4403328302
ritesh-burn commented 1 year ago

I've also seen this crash. Wrapping the Adding of items within a MainThread.InvokeOnMainThreadAsync seems to work around this crash for me. YMMV.

tranb3r commented 1 year ago

I've also seen this crash. Wrapping the Adding of items within a MainThread.InvokeOnMainThreadAsync seems to work around this crash for me. YMMV.

Does not work for me. Also, does not work on the sample provided by @EthanHipps

ritesh-burn commented 1 year ago

I've also seen this crash. Wrapping the Adding of items within a MainThread.InvokeOnMainThreadAsync seems to work around this crash for me. YMMV.

Does not work for me. Also, does not work on the sample provided by @EthanHipps

I hadn't tried the sample earlier. The following works for me as a work-around in that sample:

    [RelayCommand]
    private async Task ClearAndAddItemsAsync()
    {
        TestData.Clear();
        await Task.Delay(100);
        TestData.Add(new("Header 1", Data));
    }

The main change being adding a delay between clearing the list and adding new items.

tranb3r commented 1 year ago

I'm using ObservableRangeCollection from MvvmHelpers. So I cannot do this kind of hack easily. Is this issue going to be fixed soon ?

JonnySKK commented 1 year ago

+1 this is becoming a big issue for us

borrmann commented 1 year ago

My workaround for now is to add the group headers as items to the list and use a datatemplateselector.

Alternatively set a boolean on the first item of each group and create a template for first items that includes the group header template. That way the length of the list is not modified if that matters.

BioTurboNick commented 1 year ago

I had a similar crash. My workaround was just to recreate the ObservableCollection fresh rather than clearing and adding.

imsam67 commented 1 year ago

Same problem here and it looks like this is an inherited problem from Xamarin Forms. I don't have this issue if I use a CollectionView without grouping my data. If I do group it, my app ends up crashing.

ianpowell2017 commented 1 year ago

Same problem as @imsam67

LouwrensP commented 1 year ago

Any solution yet? I get the error when adding items to an ObservableCollection that is bound to a ListView. Using MVVM helper and adding items using the .AddRange() method. Using MVVM in .Net MAUI. Works fine in Windows and on Android.

ianpowell2017 commented 1 year ago

I solved this problem by making the collection a type that inherits from an ObservableObject, any changes to the object will then change in the collection.

LouwrensP commented 1 year ago

Hi Ian. Could you please elaborate on what you did. Did you create a class that inherited from ObservableObject or from ObservableRangeCollection?

ianpowell2017 commented 1 year ago

Yes, I created a class eg Monkey which inherits ObservableObject then created an ObservableCollection<Monkey>

LouwrensP commented 1 year ago

Doesn't work for me, same error as before :(

jbe2277 commented 1 year ago

Unfortunately, the bug still occurs with the latest MAUI version.

Environment:

How to reproduce:

Note: It works correct on:

Output:

   ObjCRuntime.ObjCException
  Message=Objective-C exception thrown.  Name: NSInternalInconsistencyException Reason: Invalid batch updates detected: the number of sections and/or items returned by the data source before and/or after performing the batch updates are inconsistent with the updates.
Data source before updates = { 9 sections with item counts: [1, 1, 1, 1, 1, 1, 1, 1, 2] }
Data source after updates = { 9 sections with item counts: [1, 1, 1, 1, 1, 1, 1, 1, 2] }
Updates = [
    Insert section 0
]
Collection view: <UICollectionView: 0x15a366200; frame = (0 0; 2388 1480); clipsToBounds = YES; autoresize = W+H; gestureRecognizers = <NSArray: 0x280a40540>; backgroundColor = UIExtendedGrayColorSpace 0 0; layer = <CALayer: 0x280ef66e0>; contentOffset: {0, 0}; contentSize: {2388, 975}; adjustedContentInset: {0, 0, 0, 0}; layout: <Microsoft_Maui_Controls_Handlers_Items_ListViewLayout: 0x153fd06e0>; dataSource: <Microsoft_Maui_Controls_Handlers_Items_ReorderableItemsViewController_1: 0x153fce370>>
Native stack trace:
    0   CoreFoundation                      0x000000019313ada0 6A07CBE8-4F65-3DBC-B59D-C1A188A7B95D + 40352
    1   libobjc.A.dylib                     0x000000018c2643d0 objc_exception_throw + 60
    2   Foundation                          0x000000018d9596cc 35567930-FBC2-374B-B44B-1DDFE3ADA4A1 + 5117644
    3   UIKitCore                           0x00000001958a1358 B0858D8E-7220-37BF-873F-ECC2B0A358C3 + 7349080
    4   UIKitCore                           0x00000001954ac5ec B0858D8E-7220-37BF-873F-ECC2B0A358C3 + 3200492
    5   UIKitCore                           0x000000019540c994 B0858D8E-7220-37BF-873F-ECC2B0A358C3 + 2546068
    6   UIKitCore                           0x000000019540c854 B0858D8E-7220-37BF-873F-ECC2B0A358C3 + 2545748
    7   NewsReader.MauiSystem               0x00000001050c30b4 _ZN16XamarinCallState3selEv + 3468
    8   NewsReader.MauiSystem               0x000000010506942c _ZN7plcrash2MS5async24dwarf_cfa_state_iteratorIyxE4nextEPjPNS1_28plcrash_dwarf_cfa_reg_rule_tEPy + 83248
    9   NewsReader.MauiSystem               0x00000001054272e8 _ZNK3icu6number23NumberFormatterSettingsINS0_24LocalizedNumberFormatterEE10toSkeletonER10UErrorCode + 2740128
    10  NewsReader.MauiSystem               0x000000010541ca6c _ZNK3icu6number23NumberFormatterSettingsINS0_24LocalizedNumberFormatterEE10toSkeletonER10UErrorCode + 2696996
    11  NewsReader.MauiSystem               0x000000010541a544 _ZNK3icu6number23NumberFormatterSettingsINS0_24LocalizedNumberFormatterEE10toSkeletonER10UErrorCode + 2687484
    12  NewsReader.MauiSystem               0x00000001053e2650 _ZNK3icu6number23NumberFormatterSettingsINS0_24LocalizedNumberFormatterEE10toSkeletonER10UErrorCode + 2458376
    13  NewsReader.MauiSystem               0x0000000105320134 _ZNK3icu6number23NumberFormatterSettingsINS0_24LocalizedNumberFormatterEE10toSkeletonER10UErrorCode + 1662444
    14  NewsReader.MauiSystem               0x0000000105323b28 _ZNK3icu6number23NumberFormatterSettingsINS0_24LocalizedNumberFormatterEE10toSkeletonER10UErrorCode + 1677280
    15  NewsReader.MauiSystem               0x000000010545fae4 _ZNK3icu6number23NumberFormatterSettingsINS0_24LocalizedNumberFormatterEE10toSkeletonER10UErrorCode + 2971548
    16  NewsReader.MauiSystem               0x000000010546d138 _ZNK3icu6number23NumberFormatterSettingsINS0_24LocalizedNumberFormatterEE10toSkeletonER10UErrorCode + 3026416
    17  Foundation                          0x000000018d4b389c 35567930-FBC2-374B-B44B-1DDFE3ADA4A1 + 243868
    18  CoreFoundation                      0x0000000193204208 6A07CBE8-4F65-3DBC-B59D-C1A188A7B95D + 864776
    19  CoreFoundation                      0x0000000193210864 6A07CBE8-4F65-3DBC-B59D-C1A188A7B95D + 915556
    20  CoreFoundation                      0x00000001931956c8 6A07CBE8-4F65-3DBC-B59D-C1A188A7B95D + 411336
    21  CoreFoundation                      0x00000001931ab1c4 6A07CBE8-4F65-3DBC-B59D-C1A188A7B95D + 500164
    22  CoreFoundation                      0x00000001931b04dc CFRunLoopRunSpecific + 612
    23  GraphicsServices                    0x00000001cdb1f35c GSEventRunModal + 164
    24  UIKitCore                           0x000000019553c37c B0858D8E-7220-37BF-873F-ECC2B0A358C3 + 3789692
    25  UIKitCore                           0x000000019553bfe0 UIApplicationMain + 340
    26  NewsReader.MauiSystem               0x0000000105080714 xamarin_UIApplicationMain + 60
    27  NewsReader.MauiSystem               0x0000000105428274 _ZNK3icu6number23NumberFormatterSettingsINS0_24LocalizedNumberFormatterEE10toSkeletonER10UErrorCode + 2744108
    28  NewsReader.MauiSystem               0x0000000105426f94 _ZNK3icu6number23NumberFormatterSettingsINS0_24LocalizedNumberFormatterEE10toSkeletonER10UErrorCode + 2739276
    29  NewsReader.MauiSystem               0x000000010541c970 _ZNK3icu6number23NumberFormatterSettingsINS0_24LocalizedNumberFormatterEE10toSkeletonER10UErrorCode + 2696744
    30  NewsReader.MauiSystem               0x000000010541a544 _ZNK3icu6number23NumberFormatterSettingsINS0_24LocalizedNumberFormatterEE10toSkeletonER10UErrorCode + 2687484
    31  NewsReader.MauiSystem               0x00000001053e2650 _ZNK3icu6number23NumberFormatterSettingsINS0_24LocalizedNumberFormatterEE10toSkeletonER10UErrorCode + 2458376
    32  NewsReader.MauiSystem               0x0000000105320134 _ZNK3icu6number23NumberFormatterSettingsINS0_24LocalizedNumberFormatterEE10toSkeletonER10UErrorCode + 1662444
    33  NewsReader.MauiSystem               0x0000000105327d44 _ZNK3icu6number23NumberFormatterSettingsINS0_24LocalizedNumberFormatterEE10toSkeletonER10UErrorCode + 1694204
    34  NewsReader.MauiSystem               0x00000001053e8760 _ZNK3icu6number23NumberFormatterSettingsINS0_24LocalizedNumberFormatterEE10toSkeletonER10UErrorCode + 2483224
    35  NewsReader.MauiSystem               0x00000001050c2110 xamarin_get_original_working_directory_path + 4016
    36  NewsReader.MauiSystem               0x000000010545aa04 _ZNK3icu6number23NumberFormatterSettingsINS0_24LocalizedNumberFormatterEE10toSkeletonER10UErrorCode + 2950844
    37  dyld                                0x00000001b2628dec 41605DC7-F412-37D1-B51B-FEE1A26701E9 + 89580

  Source=<unknown>
  StackTrace:
   at UIKit.UICollectionView.InsertSections(NSIndexSet sections)
   at Microsoft.Maui.Controls.Handlers.Items.ObservableGroupedSource.<>c__DisplayClass29_0.<Add>b__0()
   at Microsoft.Maui.Controls.Handlers.Items.ObservableGroupedSource.Update(Action update)
   at Microsoft.Maui.Controls.Handlers.Items.ObservableGroupedSource.Add(NotifyCollectionChangedEventArgs args)
   at Microsoft.Maui.Controls.Handlers.Items.ObservableGroupedSource.CollectionChanged(NotifyCollectionChangedEventArgs args)
   at Microsoft.Maui.Controls.Handlers.Items.ObservableGroupedSource.<>c__DisplayClass24_0.<CollectionChanged>b__0()
   at Foundation.NSAsyncActionDispatcher.Apply()
   at UIKit.UIApplication.UIApplicationMain(Int32 argc, String[] argv, IntPtr principalClassName, IntPtr delegateClassName)
   at UIKit.UIApplication.Main(String[] args, Type principalClass, Type delegateClass)
   at Waf.NewsReader.MauiSystem.Platforms.iOS.Program.Main(String[] args) in C:\Dev\GitHub\waf\src\NewsReader\NewsReader.MauiSystem\Platforms\iOS\Program.cs:line 9
jesshannon commented 1 year ago

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.

ghost commented 1 year ago

Hello lovely human, thank you for your comment on this issue. Because this issue has been closed for a period of time, please strongly consider opening a new issue linking to this issue instead to ensure better visibility of your comment. Thank you!