Skip to content
This repository has been archived by the owner on May 1, 2024. It is now read-only.

[Bug] NSInternalInconsistencyException exception on iOS with CollectionView #6920

Closed
yuba30 opened this issue Jul 18, 2019 · 30 comments
Closed
Labels
a/collectionview e/3 🕒 3 i/high Completely doesn't work, crashes, or is unusably slow, has no obvious workaround; occurs less often p/iOS 🍎 t/bug 🐛

Comments

@yuba30
Copy link

yuba30 commented Jul 18, 2019

Description

I have a Xamarin.Forms project that has a CollectionView that is bound to an ObservableCollection. If I call Clear() on the ObservableCollection and then call Add() to add an item I get the exception.

Steps to Reproduce

  1. Open the attached sample code.
  2. Put a break point on line 54 of MainPage.xaml.cs
  3. Run the app on a simulator.
  4. Click the button that says "Click to Generate Error" and you'll hit the breakpoint.

Expected Behavior

Item should be added.

Actual Behavior

Exception gets thrown. I was able to work around this by calling RemoveAt(0) in a while loop until the list is empty instead of using Clear() so no rush for me just wanted you guys to know about the bug.

Basic Information

  • Version with issue: Xamarin.Forms 4.1.0.581479
  • IDE: Visual Studio 2019 Enterprise
  • Platform Target Frameworks:
    • iOS: 12.2 and I'm guessing others, should apply to all

Exception

Foundation.MonoTouchException: Objective-C exception thrown. Name: NSInternalInconsistencyException Reason: Invalid update: invalid number of items in section 0. The number of items contained in an existing section after the update (1) must be equal to the number of items contained in that section before the update (1), plus or minus the number of items inserted or deleted from that section (1 inserted, 0 deleted) and plus or minus the number of items moved into or out of that section (0 moved in, 0 moved out).
Native stack trace:
0 CoreFoundation 0x000000010ffb66fb __exceptionPreprocess + 331
1 libobjc.A.dylib 0x0000000110c3bac5 objc_exception_throw + 48
2 CoreFoundation 0x000000010ffb6482 +[NSException raise:format:arguments:] + 98
3 Foundation 0x000000010bf13927 -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 194
4 UIKitCore 0x0000000119775479 -[UICollectionView _endItemAnimationsWithInvalidationContext:tentativelyForReordering:animator:] + 16127
5 UIKitCore 0x0000000119771239 -[UICollectionView _updateRowsAtIndexPaths:updateAction:updates:] + 519
6 UIKitCore 0x00000001197712c4 -[UICollectionView insertItemsAtIndexPaths:] + 64
7 CollectionsBugSample.iOS 0x0000000102d20999 xamarin_dyn_objc_msgSend + 217
8 ??? 0x00000001353135d7 0x0 + 5187384791
9 ??? 0x00000001352fd0d4 0x0 + 5187293396
10 ??? 0x00000001359a7baf 0x0 + 5194283951
11 ??? 0x0000000133dee5be 0x0 + 5165213118
12 CollectionsBugSample.iOS 0x0000000102b028a1 mono_jit_runtime_invoke + 1585
13 CollectionsBugSample.iOS 0x0000000102c139f4 mono_runtime_try_invoke + 148
14 CollectionsBugSample.iOS 0x0000000102c156af mono_runtime_invoke + 95
15 CollectionsBugSample.iOS 0x00000001029e7058 _ZL31native_to_managed_trampoline_20P11objc_objectP13objc_selectorPP11_MonoMethodj + 248
16 CollectionsBugSample.iOS 0x00000001029eb654 -[__MonoMac_NSAsyncActionDispatcher xamarinApplySelector] + 52
17 Foundation 0x000000010bfa5878 __NSThreadPerformPerform + 331
18 CoreFoundation 0x000000010ff1dbe1 CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION + 17
19 CoreFoundation 0x000000010ff1d463 __CFRunLoopDoSources0 + 243
20 CoreFoundation 0x000000010ff17b1f __CFRunLoopRun + 1231
21 CoreFoundation 0x000000010ff17302 CFRunLoopRunSpecific + 626
22 GraphicsServices 0x0000000112b092fe GSEventRunModal + 65
23 UIKitCore 0x0000000119ee2ba2 UIApplicationMain + 140
24 ??? 0x0000000135029052 0x0 + 5184327762
25 ??? 0x0000000135028de3 0x0 + 5184327139
26 ??? 0x00000001341191a3 0x0 + 5168533923
27 CollectionsBugSample.iOS 0x0000000102b028a1 mono_jit_runtime_invoke + 1585
28 CollectionsBugSample.iOS 0x0000000102c12058 mono_runtime_invoke_checked + 136
29 CollectionsBugSample.iOS 0x0000000102c176dd mono_runtime_exec_main_checked + 109
30 CollectionsBugSample.iOS 0x0000000102a6dbf7 mono_jit_exec + 311
31 CollectionsBugSample.iOS 0x0000000102d1e864 xamarin_main + 2708
32 CollectionsBugSample.iOS 0x0000000102a496f2 main + 82
33 libdyld.dylib 0x0000000112152541 start + 1

at ObjCRuntime.Runtime.ThrowNSException (System.IntPtr ns_exception) [0x00000] in /Library/Frameworks/Xamarin.iOS.framework/Versions/12.10.0.157/src/Xamarin.iOS/ObjCRuntime/Runtime.cs:406
at ObjCRuntime.Runtime.throw_ns_exception (System.IntPtr exc) [0x00000] in /Users/builder/jenkins/workspace/xamarin-macios/xamarin-macios/runtime/Delegates.generated.cs:128
at (wrapper native-to-managed) ObjCRuntime.Runtime.throw_ns_exception(intptr)
at (wrapper managed-to-native) ObjCRuntime.Messaging.void_objc_msgSend_IntPtr(intptr,intptr,intptr)
at UIKit.UICollectionView.InsertItems (Foundation.NSIndexPath[] indexPaths) [0x00022] in /Library/Frameworks/Xamarin.iOS.framework/Versions/12.10.0.157/src/Xamarin.iOS/UIKit/UICollectionView.g.cs:471
at Xamarin.Forms.Platform.iOS.ObservableItemsSource.Add (System.Collections.Specialized.NotifyCollectionChangedEventArgs args) [0x00035] in D:\a\1\s\Xamarin.Forms.Platform.iOS\CollectionView\ObservableItemsSource.cs:124
at Xamarin.Forms.Platform.iOS.ObservableItemsSource.CollectionChanged (System.Object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs args) [0x00023] in D:\a\1\s\Xamarin.Forms.Platform.iOS\CollectionView\ObservableItemsSource.cs:50
at System.Collections.ObjectModel.ObservableCollection1[T].OnCollectionChanged (System.Collections.Specialized.NotifyCollectionChangedEventArgs e) [0x00018] in /Library/Frameworks/Xamarin.iOS.framework/Versions/12.10.0.157/src/Xamarin.iOS/external/corefx/src/System.ObjectModel/src/System/Collections/ObjectModel/ObservableCollection.cs:263 at System.Collections.ObjectModel.ObservableCollection1[T].OnCollectionChanged (System.Collections.Specialized.NotifyCollectionChangedAction action, System.Object item, System.Int32 index) [0x00000] in /Library/Frameworks/Xamarin.iOS.framework/Versions/12.10.0.157/src/Xamarin.iOS/external/corefx/src/System.ObjectModel/src/System/Collections/ObjectModel/ObservableCollection.cs:338
at System.Collections.ObjectModel.ObservableCollection1[T].InsertItem (System.Int32 index, T item) [0x0001a] in /Library/Frameworks/Xamarin.iOS.framework/Versions/12.10.0.157/src/Xamarin.iOS/external/corefx/src/System.ObjectModel/src/System/Collections/ObjectModel/ObservableCollection.cs:196 at System.Collections.ObjectModel.Collection1[T].Add (T item) [0x00020] in /Library/Frameworks/Xamarin.iOS.framework/Versions/12.10.0.157/src/Xamarin.iOS/external/corefx/src/Common/src/CoreLib/System/Collections/ObjectModel/Collection.cs:73
at CollectionsBugSample.MainPage.<AddButton_Clicked>b__3_1 () [0x00013] in C:\Users\Jim\Desktop\CollectionsBugSample\CollectionsBugSample\CollectionsBugSample\MainPage.xaml.cs:50
--- End of stack trace from previous location where exception was thrown ---

at (wrapper managed-to-native) ObjCRuntime.Messaging.void_objc_msgSend_IntPtr(intptr,intptr,intptr)
at UIKit.UICollectionView.InsertItems (Foundation.NSIndexPath[] indexPaths) [0x00022] in /Library/Frameworks/Xamarin.iOS.framework/Versions/12.10.0.157/src/Xamarin.iOS/UIKit/UICollectionView.g.cs:471
at Xamarin.Forms.Platform.iOS.ObservableItemsSource.Add (System.Collections.Specialized.NotifyCollectionChangedEventArgs args) [0x00035] in D:\a\1\s\Xamarin.Forms.Platform.iOS\CollectionView\ObservableItemsSource.cs:124
at Xamarin.Forms.Platform.iOS.ObservableItemsSource.CollectionChanged (System.Object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs args) [0x00023] in D:\a\1\s\Xamarin.Forms.Platform.iOS\CollectionView\ObservableItemsSource.cs:50
at System.Collections.ObjectModel.ObservableCollection1[T].OnCollectionChanged (System.Collections.Specialized.NotifyCollectionChangedEventArgs e) [0x00018] in /Library/Frameworks/Xamarin.iOS.framework/Versions/12.10.0.157/src/Xamarin.iOS/external/corefx/src/System.ObjectModel/src/System/Collections/ObjectModel/ObservableCollection.cs:263 at System.Collections.ObjectModel.ObservableCollection1[T].OnCollectionChanged (System.Collections.Specialized.NotifyCollectionChangedAction action, System.Object item, System.Int32 index) [0x00000] in /Library/Frameworks/Xamarin.iOS.framework/Versions/12.10.0.157/src/Xamarin.iOS/external/corefx/src/System.ObjectModel/src/System/Collections/ObjectModel/ObservableCollection.cs:338
at System.Collections.ObjectModel.ObservableCollection1[T].InsertItem (System.Int32 index, T item) [0x0001a] in /Library/Frameworks/Xamarin.iOS.framework/Versions/12.10.0.157/src/Xamarin.iOS/external/corefx/src/System.ObjectModel/src/System/Collections/ObjectModel/ObservableCollection.cs:196 at System.Collections.ObjectModel.Collection1[T].Add (T item) [0x00020] in /Library/Frameworks/Xamarin.iOS.framework/Versions/12.10.0.157/src/Xamarin.iOS/external/corefx/src/Common/src/CoreLib/System/Collections/ObjectModel/Collection.cs:73
at CollectionsBugSample.MainPage.<AddButton_Clicked>b__3_1 () [0x00013] in C:\Users\Jim\Desktop\CollectionsBugSample\CollectionsBugSample\CollectionsBugSample\MainPage.xaml.cs:50 0 CoreFoundation 0x000000010ffb66fb __exceptionPreprocess + 331
1 libobjc.A.dylib 0x0000000110c3bac5 objc_exception_throw + 48
2 CoreFoundation 0x000000010ffb6482 +[NSException raise:format:arguments:] + 98
3 Foundation 0x000000010bf13927 -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 194
4 UIKitCore 0x0000000119775479 -[UICollectionView _endItemAnimationsWithInvalidationContext:tentativelyForReordering:animator:] + 16127
5 UIKitCore 0x0000000119771239 -[UICollectionView _updateRowsAtIndexPaths:updateAction:updates:] + 519
6 UIKitCore 0x00000001197712c4 -[UICollectionView insertItemsAtIndexPaths:] + 64
7 CollectionsBugSample.iOS 0x0000000102d20999 xamarin_dyn_objc_msgSend + 217
8 ??? 0x00000001353135d7 0x0 + 5187384791
9 ??? 0x00000001352fd0d4 0x0 + 5187293396
10 ??? 0x00000001359a7baf 0x0 + 5194283951
11 ??? 0x0000000133dee5be 0x0 + 5165213118
12 CollectionsBugSample.iOS 0x0000000102b028a1 mono_jit_runtime_invoke + 1585
13 CollectionsBugSample.iOS 0x0000000102c139f4 mono_runtime_try_invoke + 148
14 CollectionsBugSample.iOS 0x0000000102c156af mono_runtime_invoke + 95
15 CollectionsBugSample.iOS 0x00000001029e7058 _ZL31native_to_managed_trampoline_20P11objc_objectP13objc_selectorPP11_MonoMethodj + 248
16 CollectionsBugSample.iOS 0x00000001029eb654 -[__MonoMac_NSAsyncActionDispatcher xamarinApplySelector] + 52
17 Foundation 0x000000010bfa5878 __NSThreadPerformPerform + 331
18 CoreFoundation 0x000000010ff1dbe1 CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION + 17
19 CoreFoundation 0x000000010ff1d463 __CFRunLoopDoSources0 + 243
20 CoreFoundation 0x000000010ff17b1f __CFRunLoopRun + 1231
21 CoreFoundation 0x000000010ff17302 CFRunLoopRunSpecific + 626
22 GraphicsServices 0x0000000112b092fe GSEventRunModal + 65
23 UIKitCore 0x0000000119ee2ba2 UIApplicationMain + 140
24 ??? 0x0000000135029052 0x0 + 5184327762
25 ??? 0x0000000135028de3 0x0 + 5184327139
26 ??? 0x00000001341191a3 0x0 + 5168533923
27 CollectionsBugSample.iOS 0x0000000102b028a1 mono_jit_runtime_invoke + 1585
28 CollectionsBugSample.iOS 0x0000000102c12058 mono_runtime_invoke_checked + 136
29 CollectionsBugSample.iOS 0x0000000102c176dd mono_runtime_exec_main_checked + 109
30 CollectionsBugSample.iOS 0x0000000102a6dbf7 mono_jit_exec + 311
31 CollectionsBugSample.iOS 0x0000000102d1e864 xamarin_main + 2708
32 CollectionsBugSample.iOS 0x0000000102a496f2 main + 82
33 libdyld.dylib 0x0000000112152541 start + 1
CollectionsBugSample.zip

@yuba30 yuba30 added s/unverified New report that has yet to be verified t/bug 🐛 labels Jul 18, 2019
@pauldipietro pauldipietro added this to New in Triage Jul 18, 2019
@jsuarezruiz
Copy link
Contributor

Thanks for the sample. I reproduce the problem (in Xamarin.Forms 4.x, tested in 4.1 and 4.2 pre).

Captura de pantalla 2019-07-19 a las 10 17 53

Without cleaning the collection it works as expected (adding a new element).

@jsuarezruiz jsuarezruiz moved this from New to Needs Estimate in Triage Jul 19, 2019
@jsuarezruiz jsuarezruiz removed the s/unverified New report that has yet to be verified label Jul 19, 2019
@adrianknight89
Copy link
Contributor

adrianknight89 commented Jul 20, 2019

I haven't looked at your code, but I remember getting a similar exception when the items collection was being updated on a thread other than the main UI thread. If you are not doing this, the renderer might be doing it.

@afk013
Copy link

afk013 commented Jul 22, 2019

Hi @adrianknight89 I have the same issue (I commented on #5793) for my example repo I invoke on the main thread and still have the issue, if you any other ideas it would be most appreciated, thanks.

@adrianknight89
Copy link
Contributor

@afk013 You're calling Clear() in the button click handler, which should be on the UI thread already, so I don't think there is a need for the extra BeginInvokeOnMainThread() call. What I meant to say was that the underlying native renderers should try to update the data source on the UI thread (though this may or may not be necessary if the root cause of the issue can be fixed elsewhere). According to @hartez, #5793 is fixed. Can you use the latest pre-release to test your code again?

@afk013
Copy link

afk013 commented Jul 22, 2019

Hi @adrianknight89 sorry yes the BeginInvokeOnMainThread() is unnecessarily in this instance, the repro was quickly thrown together as a minimum viable project to demonstrate the issue. I have updated to the latest pre-release (4.2.0.608146-pre1) and the issue still persists. I have tested this on iPhone Xs and iPhone6s.

@ronnieoverby
Copy link

ronnieoverby commented Jul 22, 2019

I'm observing this issue in Forms 4.1.0.581479

As long as I clear the collection by some other means than .Clear() I don't encounter the exception.

For example:

while (Things.Count > 0)
  Things.RemoveAt(0);

@samhouts samhouts added this to Backlog in CollectionView Jul 25, 2019
@samhouts samhouts added i/high Completely doesn't work, crashes, or is unusably slow, has no obvious workaround; occurs less often e/3 🕒 3 labels Jul 29, 2019
@samhouts samhouts moved this from Needs Estimate to Ready For Work in Triage Jul 29, 2019
@samhouts samhouts added this to To do in iOS Ready For Work Jul 30, 2019
@samhouts samhouts removed this from Ready For Work in Triage Jul 30, 2019
@hartez
Copy link
Contributor

hartez commented Jul 30, 2019

Fixed in 4.2.0.618605-pre2.

@hartez hartez closed this as completed Jul 30, 2019
iOS Ready For Work automation moved this from To do to Done Jul 30, 2019
@samhouts samhouts moved this from Backlog to Done in CollectionView Aug 1, 2019
@MarMarIV
Copy link

I have NuGet version 4.2.0.618605-pre2 and still same problem.

@oPhamThanhHai
Copy link

oPhamThanhHai commented Aug 29, 2019

Fixed in 4.2.0.618605-pre2.

Sorry, but i've test but it still reproduction. (version 4.2.0.709249)
When I using Clear or Add in ItemSource (call await when Add item into ItemSource), �it'll �throw exception.
Please help me!

@gabrielfreire
Copy link

im having this problem for .Add, .Remove, .Insert and .Clear methods, only for iOS, Xamarin.Forms 4.3.0.991211

@JohnLivermore
Copy link

Same issue here. Version 4.3.0.991221.

@hartez
Copy link
Contributor

hartez commented Dec 13, 2019

@gabrielfreire and @JohnLivermore are you using grouping by any chance?

@JohnLivermore
Copy link

No. We are using it in it's most basic form as a vertically displayed list binding to a List. We get the exception when switching the root navigation to a different stack.

@hartez
Copy link
Contributor

hartez commented Dec 13, 2019

@JohnLivermore could you please open a separate issue for your case? That sounds like a different case than what this issue addressed.

@kumarc-123
Copy link

im having this problem for .Add, .Remove, .Insert and .Clear methods, only for iOS, Xamarin.Forms 4.3.0.991211

Even I'm having this issue with Xamarin Forms 4.4

@kumarc-123
Copy link

kumarc-123 commented Jan 7, 2020

This issue is still present. I tried to add items in UI thread too. But still not working. This is occurring in all iOS versions below iOS 13.

<PackageReference Include="Xamarin.Forms" Version="4.4.0.991265" />

@samhouts samhouts removed this from Done in iOS Ready For Work May 6, 2020
@samhouts samhouts removed this from Done in CollectionView May 6, 2020
@tonyfonager
Copy link

I have the same issue on XF 5.0.0.2012

1 similar comment
@hardikphd
Copy link

I have the same issue on XF 5.0.0.2012

@indyjason79
Copy link

This issue should be re-opened. I'm still having the issue on XF 5.0.0.2012.

@rwaldenfraedom
Copy link

rwaldenfraedom commented Apr 22, 2021

Yes this is happen to me as well after updating to 5.0.0.2012. The same code was working fine on 4.8.0.1687.

If it helps, i'm using a using a grid layout when this crash happens.

@LucaZiegler
Copy link

Same issue, switched from ListView to CollectionView 😔

@kalprajkakani
Copy link

Why this issue is closed? Is this issue resolved or any workaround?

@Nadraw
Copy link

Nadraw commented May 20, 2021

I would like to add that this also started happening to me after upgrading to 5.0.0.2012. It was working fine on 4.8.0.1451. I am using grouping in the cases where this error occurs. Without grouping, all seems to work fine. But grouping is a requirement. I tried using RemoveAt/Insert instead of Clear/Add, but no difference. Also tried BeginInvokeOnMainThread suggestion and still get the error. It does feel like something broke in XF 5.0

@michaldivis
Copy link

@Nadraw I'm having the same problem with Xamarin.Forms 5.0.0.2012. CollectionView throws this exception on iOS when using grouping.

@RenatGaliew
Copy link

The problem has not been fixed yet

@CodeAuChocolat
Copy link

See also #14362.

A workaround for me was to initialize the ObservableCollection with one element. Any subsequent Add worked fine. The add of the first item of a grouped colllection is handled differently.

@SeriyGO
Copy link

SeriyGO commented Oct 16, 2021

Its same to me. Iv got same probllems. In all Applications, Android works great but IOS always catching some problems.

@Filskov-IT
Copy link

I'm getting this problem too. But only on iOS

@DamienDoumer
Copy link

We're in 2022, and I'm still getting this issue in prod. version 5.0.0.2337

@GV1072
Copy link

GV1072 commented Oct 11, 2022

Hi all,
I am using Xamarin Forms latest version 5.0.0.2515 but still I am getting same issue in ios. Please let me know if there is any solution from framework without using delay 500.

Thank you in advance.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
a/collectionview e/3 🕒 3 i/high Completely doesn't work, crashes, or is unusably slow, has no obvious workaround; occurs less often p/iOS 🍎 t/bug 🐛
Projects
None yet
Development

No branches or pull requests