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

[Bug] Xamarin.Forms Observablecollection throws exception on ios device #7795

Closed
dnehl opened this issue Oct 3, 2019 · 15 comments
Closed

[Bug] Xamarin.Forms Observablecollection throws exception on ios device #7795

dnehl opened this issue Oct 3, 2019 · 15 comments
Labels
s/needs-info ❓ A question has been asked that requires an answer before work can continue on this issue. s/needs-repro ❔ This reported issue doesn't include a sample project reproducing the issue. Please provide one. s/unverified New report that has yet to be verified t/bug 🐛

Comments

@dnehl
Copy link

dnehl commented Oct 3, 2019

Description

In the view model I have a observablecollection which will be created in the constructor. In the method I call the RefreshCommand, which doing the following steps:

Clears the existing list (Items.Clear())
Calls a webservice to receive new Items (async)
uses foreach to add all new Itms to the list
This items will be displayed in a ListView and this works great for Android (simulator and real device) and also for iOS Simulator, but as soon as I deploy it on a real iOS device (in this case iPhone 6), the appliction crashes..

Here is a part of the exception:

"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). "

I already tried it to move the clear and fill action in the GUIThread and I also tried it with a thread safe collection: https://codetraveler.io/2019/09/11/using-observablecollection-in-a-multi-threaded-xamarin-forms-application/

but the application crashes again and again.. The workaround which works for me is to change the observablecollection to a normal list...

Steps to Reproduce

  1. Create a view with an ListView and bind this ListView to an ObservableCollection
  2. Reload the items (e.g. service)
  3. Clear the collection with the method .Clear()
  4. adds the new items with .Add(newItem) to the collection

Expected Behavior

the list should removes the old items and add the new ones.
It works on Android (device, simualtor) and iOS Simulator

Actual Behavior

with an iOS Device (iphone 6) the app crashes with the following exception:
"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). "

Basic Information

  • Version with issue: latest xamarin preview
  • Last known good version: -
  • IDE: Visual studio
  • Platform Target Frameworks:
    • iOS: latest
    • Android: Android 10
  • Nuget Packages:
  • Affected Devices: iPhone 6
@dnehl dnehl added s/unverified New report that has yet to be verified t/bug 🐛 labels Oct 3, 2019
@DiegoFaFe
Copy link

DiegoFaFe commented Oct 3, 2019

Seems like duplicated of #5793 #6011 #6920 and related to #7678

PD: Prob will be fixed by #7711

@PureWeen
Copy link
Contributor

PureWeen commented Oct 3, 2019

@WaiseFass can you attach a repro please?

I tried your steps on a physical device and didn't have any issues

   await Task.Delay(5000);

            collection.Clear();
            Enumerable.Range(0, 1000).ToList().ForEach(x => collection.Add(x.ToString()));


            await Task.Delay(5000);
            collection.Clear();
            collection.Add("1");

@PureWeen PureWeen added s/needs-info ❓ A question has been asked that requires an answer before work can continue on this issue. s/needs-repro ❔ This reported issue doesn't include a sample project reproducing the issue. Please provide one. labels Oct 3, 2019
@samhouts
Copy link
Member

samhouts commented Nov 4, 2019

@WaiseFass Since we haven't heard from you in more than 30 days, we hope this issue is no longer affecting you. If it is, please reopen this issue and provide the requested information so that we can look into it further. Thank you!

@samhouts samhouts closed this as completed Nov 4, 2019
@LuisEGomezV
Copy link

This is not solved, I can reproduce this error. Please reopen the issue

1 similar comment
@ghaith100994
Copy link

This is not solved, I can reproduce this error. Please reopen the issue

@mdmota
Copy link

mdmota commented Nov 5, 2019

This is not solved. iOS 12 using collectionview

@jfversluis
Copy link
Member

If you can reproduce, please add a project showing this behavior

@ghaith100994
Copy link

Test.zip

@jfversluis This Is the Sample you want

@ghaith100994
Copy link

ghaith100994 commented Nov 5, 2019

and this is the error message

"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 (2) must be equal to the number of items contained in that section before the update (2), 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).\nNative stack trace:\n\t0   CoreFoundation                      0x00000001caecb198 <redacted> + 252\n\t1   libobjc.A.dylib                     0x00000001ca0a39f8 objc_exception_throw + 56\n\t2   CoreFoundation                      0x00000001cade488c <redacted> + 0\n\t3   Foundation                          0x00000001cb8a8b38 <redacted> + 112\n\t4   UIKitCore                           0x00000001f6fc3324 <redacted> + 12480\n\t5   UIKitCore                           0x00000001f6fca1b8 <redacted> + 92\n\t6   UIKitCore                           0x00000001f6fca4ec <redacted> + 384\n\t7   UIKitCore                           0x00000001f6fca34c <redacted> + 96\n\t8   UIKitCore                           0x00000001f6fca2d0 <redacted> + 84\n\t9   UIKitCore                           0x00000001f6fca20c <redacted> + 64\n\t10  IPTV.Xamarin.iOS                    0x00000001010c64b4 IPTV.Xamarin.iOS + 15115444\n\t11  IPTV.Xamarin.iOS                    0x000000010103ff98 IPTV.Xamarin.iOS + 14565272\n\t12  IPTV.Xamarin.iOS                    0x00000001017f3bb0 IPTV.Xamarin.iOS + 22641584\n\t13  IPTV.Xamarin.iOS                    0x00000001017f314c IPTV.Xamarin.iOS + 22638924\n\t14  IPTV.Xamarin.iOS                    0x0000000100b3257c IPTV.Xamarin.iOS + 9266556\n\t15  IPTV.Xamarin.iOS                    0x0000000100b32750 IPTV.Xamarin.iOS + 9267024\n\t16  IPTV.Xamarin.iOS                    0x0000000100b321e0 IPTV.Xamarin.iOS + 9265632\n\t17  IPTV.Xamarin.iOS                    0x00000001004ef91c IPTV.Xamarin.iOS + 2701596\n\t18  IPTV.Xamarin.iOS                    0x00000001008112c0 IPTV.Xamarin.iOS + 5984960\n\t19  IPTV.Xamarin.iOS                    0x000000010042e43c IPTV.Xamarin.iOS + 1909820\n\t20  IPTV.Xamarin.iOS                    0x00000001003b6590 IPTV.Xamarin.iOS + 1418640\n\t21  IPTV.Xamarin.iOS                    0x00000001003b639c IPTV.Xamarin.iOS + 1418140\n\t22  IPTV.Xamarin.iOS                    0x000000010042e2fc IPTV.Xamarin.iOS + 1909500\n\t23  IPTV.Xamarin.iOS                    0x00000001003d3074 IPTV.Xamarin.iOS + 1536116\n\t24  IPTV.Xamarin.iOS                    0x00000001003d3198 IPTV.Xamarin.iOS + 1536408\n\t25  IPTV.Xamarin.iOS                    0x00000001003d2430 IPTV.Xamarin.iOS + 1532976\n\t26  IPTV.Xamarin.iOS                    0x00000001003ccf68 IPTV.Xamarin.iOS + 1511272\n\t27  IPTV.Xamarin.iOS                    0x00000001003cb390 IPTV.Xamarin.iOS + 1504144\n\t28  IPTV.Xamarin.iOS                    0x00000001003c62e8 IPTV.Xamarin.iOS + 1483496\n\t29  IPTV.Xamarin.iOS                    0x000000010042cbf0 IPTV.Xamarin.iOS + 1903600\n\t30  IPTV.Xamarin.iOS                    0x0000000100839348 IPTV.Xamarin.iOS + 6148936\n\t31  IPTV.Xamarin.iOS                    0x000000010042e43c IPTV.Xamarin.iOS + 1909820\n\t32  IPTV.Xamarin.iOS                    0x00000001003b6590 IPTV.Xamarin.iOS + 1418640\n\t33  IPTV.Xamarin.iOS                    0x00000001003b639c IPTV.Xamarin.iOS + 1418140\n\t34  IPTV.Xamarin.iOS                    0x000000010042e2fc IPTV.Xamarin.iOS + 1909500\n\t35  IPTV.Xamarin.iOS                    0x00000001003d3074 IPTV.Xamarin.iOS + 1536116\n\t36  IPTV.Xamarin.iOS                    0x00000001003d3198 IPTV.Xamarin.iOS + 1536408\n\t37  IPTV.Xamarin.iOS                    0x00000001003d2430 IPTV.Xamarin.iOS + 1532976\n\t38  IPTV.Xamarin.iOS                    0x00000001003ccf68 IPTV.Xamarin.iOS + 1511272\n\t39  IPTV.Xamarin.iOS                    0x00000001003cb390 IPTV.Xamarin.iOS + 1504144\n\t40  IPTV.Xamarin.iOS                    0x00000001003c62e8 IPTV.Xamarin.iOS + 1483496\n\t41  IPTV.Xamarin.iOS                    0x000000010042cbf0 IPTV.Xamarin.iOS + 1903600\n\t42  IPTV.Xamarin.iOS                    0x000000010084adf4 IPTV.Xamarin.iOS + 6221300\n\t43  IPTV.Xamarin.iOS                    0x000000010042e43c IPTV.Xamarin.iOS + 1909820\n\t44  IPTV.Xamarin.iOS                    0x00000001003b6590 IPTV.Xamarin.iOS + 1418640\n\t45  IPTV.Xamarin.iOS                    0x00000001003b639c IPTV.Xamarin.iOS + 1418140\n\t46  IPTV.Xamarin.iOS                    0x000000010042e2fc IPTV.Xamarin.iOS + 1909500\n\t47  IPTV.Xamarin.iOS                    0x00000001003d3074 IPTV.Xamarin.iOS + 1536116\n\t48  IPTV.Xamarin.iOS                    0x00000001003d3198 IPTV.Xamarin.iOS + 1536408\n\t49  IPTV.Xamarin.iOS                    0x00000001003d2430 IPTV.Xamarin.iOS + 1532976\n\t50  IPTV.Xamarin.iOS                    0x00000001003ccf68 IPTV.Xamarin.iOS + 1511272\n\t51  IPTV.Xamarin.iOS                    0x00000001003cb390 IPTV.Xamarin.iOS + 1504144\n\t52  IPTV.Xamarin.iOS                    0x00000001003c62e8 IPTV.Xamarin.iOS + 1483496\n\t53  IPTV.Xamarin.iOS                    0x000000010042cbf0 IPTV.Xamarin.iOS + 1903600\n\t54  IPTV.Xamarin.iOS                    0x00000001019eaccc IPTV.Xamarin.iOS + 24702156\n\t55  IPTV.Xamarin.iOS                    0x000000010042e43c IPTV.Xamarin.iOS + 1909820\n\t56  IPTV.Xamarin.iOS                    0x00000001003b6590 IPTV.Xamarin.iOS + 1418640\n\t57  IPTV.Xamarin.iOS                    0x00000001003b639c IPTV.Xamarin.iOS + 1418140\n\t58  IPTV.Xamarin.iOS                    0x000000010042e2fc IPTV.Xamarin.iOS + 1909500\n\t59  IPTV.Xamarin.iOS                    0x00000001003d3074 IPTV.Xamarin.iOS + 1536116\n\t60  IPTV.Xamarin.iOS                    0x00000001003d3198 IPTV.Xamarin.iOS + 1536408\n\t61  IPTV.Xamarin.iOS                    0x00000001003d2430 IPTV.Xamarin.iOS + 1532976\n\t62  IPTV.Xamarin.iOS                    0x00000001003ccf68 IPTV.Xamarin.iOS + 1511272\n\t63  IPTV.Xamarin.iOS                    0x00000001003cb390 IPTV.Xamarin.iOS + 1504144\n\t64  IPTV.Xamarin.iOS                    0x00000001003c62e8 IPTV.Xamarin.iOS + 1483496\n\t65  IPTV.Xamarin.iOS                    0x000000010042cbf0 IPTV.Xamarin.iOS + 1903600\n\t66  IPTV.Xamarin.iOS                    0x00000001019ea2c4 IPTV.Xamarin.iOS + 24699588\n\t67  IPTV.Xamarin.iOS                    0x000000010042e43c IPTV.Xamarin.iOS + 1909820\n\t68  IPTV.Xamarin.iOS                    0x00000001003b6590 IPTV.Xamarin.iOS + 1418640\n\t69  IPTV.Xamarin.iOS                    0x00000001003b639c IPTV.Xamarin.iOS + 1418140\n\t70  IPTV.Xamarin.iOS                    0x000000010042e2fc IPTV.Xamarin.iOS + 1909500\n\t71  IPTV.Xamarin.iOS                    0x00000001003d2868 IPTV.Xamarin.iOS + 1534056\n\t72  IPTV.Xamarin.iOS                    0x0000000101084e4c IPTV.Xamarin.iOS + 14847564\n\t73  IPTV.Xamarin.iOS                    0x0000000100630ac4 IPTV.Xamarin.iOS + 4016836\n\t74  IPTV.Xamarin.iOS                    0x0000000102559070 mono_pmip + 28164\n\t75  IPTV.Xamarin.iOS                    0x0000000102615ab8 mono_pmip + 800844\n\t76  IPTV.Xamarin.iOS                    0x0000000102619354 mono_pmip + 815336\n\t77  IPTV.Xamarin.iOS                    0x0000000100286cd8 IPTV.Xamarin.iOS + 175320\n\t78  IPTV.Xamarin.iOS                    0x000000010028933c IPTV.Xamarin.iOS + 185148\n\t79  Foundation                          0x00000001cb952690 <redacted> + 336\n\t80  CoreFoundation                      0x00000001cae5cf1c <redacted> + 24\n\t81  CoreFoundation                      0x00000001cae5ce9c <redacted> + 88\n\t82  CoreFoundation                      0x00000001cae5c784 <redacted> + 176\n\t83  CoreFoundation                      0x00000001cae576c0 <redacted> + 1004\n\t84  CoreFoundation                      0x00000001cae56fb4 CFRunLoopRunSpecific + 436\n\t85  GraphicsServices                    0x00000001cd05879c GSEventRunModal + 104\n\t86  UIKitCore                           0x00000001f76b9c38 UIApplicationMain + 212\n\t87  IPTV.Xamarin.iOS                    0x00000001010d8de0 IPTV.Xamarin.iOS + 15191520\n\t88  IPTV.Xamarin.iOS                    0x000000010103ad4c IPTV.Xamarin.iOS + 14544204\n\t89  IPTV.Xamarin.iOS                    0x000000010103acd4 IPTV.Xamarin.iOS + 14544084\n\t90  IPTV.Xamarin.iOS                    0x00000001002c11f0 IPTV.Xamarin.iOS + 414192\n\t91  IPTV.Xamarin.iOS                    0x0000000100630ac4 IPTV.Xamarin.iOS + 4016836\n\t92  IPTV.Xamarin.iOS                    0x0000000102559070 mono_pmip + 28164\n\t93  IPTV.Xamarin.iOS                    0x0000000102615ab8 mono_pmip + 800844\n\t94  IPTV.Xamarin.iOS                    0x000000010261bd80 mono_pmip + 826132\n\t95  IPTV.Xamarin.iOS                    0x0000000102537360 IPTV.Xamarin.iOS + 36549472\n\t96  IPTV.Xamarin.iOS                    0x00000001027238a0 _Z9__isctypeim + 59328\n\t97  IPTV.Xamarin.iOS                    0x00000001002c10f0 IPTV.Xamarin.iOS + 413936\n\t98  libdyld.dylib                       0x00000001ca91a8e0 <redacted> + 4\n"

@ghaith100994
Copy link

I just solve it by adding await task.delay(10); after every item added

@RhomGit
Copy link

RhomGit commented Nov 11, 2019

My iOS users have just started reporting this after our release on the weekend. urgh
I will try the task.delay, thanks for your input.

image

@m3talelite
Copy link

I'm still experiencing this issue. Task.delay is not a work around for my use case, since sometimes I need to add over 100 to 1000 items. If I were to use the "workaround" task.delay(10) the users would have to wait 10 seconds just for a simple list to load. Any other suggestions?

@DiegoFaFe
Copy link

I'm still experiencing this issue. Task.delay is not a work around for my use case, since sometimes I need to add over 100 to 1000 items. If I were to use the "workaround" task.delay(10) the users would have to wait 10 seconds just for a simple list to load. Any other suggestions?

@m3talelite Is it still happening in the last version of XF 4.3?

@m3talelite
Copy link

It's still happening on the current version of XF 4.3 it happens when we insert an item into an observable collection at any random position. The same happens with the RemoveAt when we clear one of our observable collections backwards. We tried in different ways, like accessing it from different threads and the main thread of course but the issue is still occurring

@Markus2807
Copy link

I got the same error on my Xamarin.Forms project.
I got rid of this error if i added "Device.BeginInvokeOnMainThread(() =>"

Device.BeginInvokeOnMainThread(() =>
            {
                SettingsList.ToList().ForEach(x =>
                {
                    var FoundItem = x.FirstOrDefault(search => search.ItemSettings == SettingsItem.ItemSettings);
                    if (FoundItem != null)
                    {
                        try
                        {
                            x.Remove(FoundItem);
                            return;
                        }
                        catch (Exception ex)
                        {
                            DebugWriter.WriteToDebugger($"Error deleting items from List: {ex.HelpLink}\n{ex.Message}");
                        }
                    }
                });

            });

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
s/needs-info ❓ A question has been asked that requires an answer before work can continue on this issue. s/needs-repro ❔ This reported issue doesn't include a sample project reproducing the issue. Please provide one. s/unverified New report that has yet to be verified t/bug 🐛
Projects
None yet
Development

No branches or pull requests