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.
Item should be added.
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.
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:a1sXamarin.Forms.Platform.iOSCollectionViewObservableItemsSource.cs:124
at Xamarin.Forms.Platform.iOS.ObservableItemsSource.CollectionChanged (System.Object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs args) [0x00023] in D:a1sXamarin.Forms.Platform.iOSCollectionViewObservableItemsSource.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.ObservableCollection
1[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.Collection
1[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.
--- 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:a1sXamarin.Forms.Platform.iOSCollectionViewObservableItemsSource.cs:124
at Xamarin.Forms.Platform.iOS.ObservableItemsSource.CollectionChanged (System.Object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs args) [0x00023] in D:a1sXamarin.Forms.Platform.iOSCollectionViewObservableItemsSource.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.ObservableCollection
1[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.Collection
1[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.
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
Thanks for the sample. I reproduce the problem (in Xamarin.Forms 4.x, tested in 4.1 and 4.2 pre).
Without cleaning the collection it works as expected (adding a new element).
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.
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.
@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?
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.
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:
C#
while (Things.Count > 0)
Things.RemoveAt(0);
Fixed in 4.2.0.618605-pre2.
I have NuGet version 4.2.0.618605-pre2 and still same problem.
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!
im having this problem for .Add, .Remove, .Insert and .Clear
methods, only for iOS, Xamarin.Forms 4.3.0.991211
Same issue here. Version 4.3.0.991221.
@gabrielfreire and @JohnLivermore are you using grouping by any chance?
No. We are using it in it's most basic form as a vertically displayed list binding to a List
@JohnLivermore could you please open a separate issue for your case? That sounds like a different case than what this issue addressed.
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
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" />
Most helpful comment
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" />