Xamarin.forms: [iOS] ListView throws Exception on ObservableCollection.Add/Remove

Created on 7 Dec 2017  路  26Comments  路  Source: xamarin/Xamarin.Forms

Original: https://bugzilla.xamarin.com/show_bug.cgi?id=60813

Description

When a ListView is in a Page that is not activated (e.g. not active tab inside a TabbedPage) and its ItemsSource is updated it throws an error.

Steps to Reproduce

  1. The attached sample shows a TabbedPage with each tab containing a ListView
  2. Tap "Add1" MenuAction
  3. Item is added to the ListView
  4. Tap "Add2" MenuAction
  5. Crash

Alternative steps

  1. Navigate to 2nd tab
  2. Navigate back to 1st tab
  3. Tap "Add2" MenuAction
  4. Item is added to the 2nd ListVIew
  5. Conclusion: Doesn't crash when navigated first

Expected Behavior

The ListViews should receive an item for each tap on the MenuActions

Actual Behavior

When the ListView was not navigated first, it throws an Exception

Basic Information

  • Version with issue: 2.4.280, 2.5.0.91635
  • Last known good version: N/A
  • IDE: VS15 Windows
  • Platform Target Frameworks:

    • iOS: 9

  • Affected Devices: Simulator and iPad Mini 3 both running iOS 11.1

Reproduction Link

FormsIOSListViewCrash.zip

Workaround

Currently you have to somehow programmatically make the ListView appear before adding an item. If the ListView is inside a tab that is not visible, show it by setting the CurrentPage Property.
If it is in a MasterDetailPage, you could try showing it by toggling the IsPresented Property.

iOS 馃崕 bug

Most helpful comment

It's crashing for me on my iPad. I'll take a look at this one and see what I can figure out

All 26 comments

Fairly confident we've seen this too because the symptoms and cause very closely match your description. In our case it's happening with a MasterDetail where the Detail isn't currently visible by the ItemSource of the ListView is updated.

@mattclarkie For my case I found a workaround to programmatically show the page before updating the list. Maybe you can to the same with your detail page.

Thanks @bruzkovsky I spent long enough just finding the issue, I probably would have blown a brain cell trying to find a workaround myself. 馃憤

When is this gonna be fixed? Any work around atm?

@Sokoo92 I added my workaround to the description, hope it can help you out...

Anyhow, will we get an update on this soon? @StephaneDelcroix @jassmith

@bruzkovsky awesome! Thanks :) I'll navigate to the Tab before adding it to the Collection :)

Probably related to #1927

Hit by this bug too even with the latest XF 2.5.1.444934
Workaround from @bruzkovsky indeed works but is ugly.
Any ETA for fixing this?
Thanks

@nbsoftware I Just tested it with 3.0.0.550146 it works.
Could you confirm?

Nope, still reproducible in 3.0.0.550146

Updated Repro:
FormIOSListViewCrashNew.zip

@bruzkovsky I have tried all of your attached samples, including the one you provided on your fork (via the Bugzilla ticket), and I cannot get it to crash! I'm wondering if there is another variable that we need to identify. What devices/simulators have you tried recently? What version of VS are you currently on?

My IDE version info: https://gist.github.com/bruzkovsky/e16cb0b58f81f8b045a426a2406d68ba

I tried with the iPhone 6s Simulator running iOS 11.2 and on my iPad3 Mini.

Did you follow the steps exactly? You could also try to just tap "Add2" first thing after the app starts.

I can confirm @bruzkovsky's sample does still crash, even with latest XF 3.0.561731.
iPhone 8 Simulator running iOS 11.4

@nbsoftware Are you also on VS 2017 15.7.1?

I'm using VS 2017 15.7.2 with Xamarin.iOS 11.10.1.178, building on iOS 11.3 SDK with Xcode 9.3 9E145.

I have followed the instructions exactly with the same simulators, but still no luck! I am also unable to reproduce the errors in #1542 or #1927 anymore, and I believe they all have the same root cause.

I updated to VS 2017 15.7.3 with Xamarin-iOS 11.12.0.4 and can still reproduce the crash. Really strange...

Here is the stack trace:
https://gist.github.com/bruzkovsky/ff216b9c7d6604f5792bede97dad6d07

Also tried it directly on the Mac with VS for Mac 7.5.2, stack trace below:
https://gist.github.com/bruzkovsky/9a3fd2aeb6858bd82a4935a621aca924

It's crashing for me on my iPad. I'll take a look at this one and see what I can figure out

I still get same issue on iPadAir (did not test on other devices)

Same issue on ios 12.1 and iphone simulator XR...

Still experiencing same issue with 3.6.0.344457.
Listview binded to ObservableCollection inside a tab page. Throws the same error on the first object added to the collection.

Same issue here with 4.0.0.425677. ListView inside a Shell Tab. Working without problems on Android.

@MSiccDev can you try the latest 4.1 pre and if you still have an issue create an issue with a repro?

@PureWeen before reading your comment, I moved on. I was calling .Clear() on my ObservableCollection(s) before adding new items (as I planned to simply remove items and insert the updated list after that). The collection count was 0, and the error I got indicated that this is a problem on iOS. I moved to a check with .All(vm => vm.Id != whatever.Id), which makes the error disappearing. I am trying to avoid preview packages in the current state as much as I can, but the error happened also with the latest public release of XF. Hope this feedback helps to narrow down the issue.

@PureWeen, currently using XF version 4.1.0.673156 (latest version) and the issue persists:

Shell Tab App with a CollectionView attached to an Observable Collection.

When calling .Add in my ViewModel the App Crashes:

"UIKit Consistency error: you are calling a UIKit method that can only be invoked from the UI thread."

@LuisDeHaro you have to call .Add on the Main Thread, for example by using await Device.InvokeOnMainThreadAsync(() => myObservableCollection.Add(myItem))

It;s 2020. The latest & greatest VisualStudio, XamForms, etc. This issue (or a similar one) still exists with CollectionView and ObservableCollection. In some scenarios, index out of range occurs -- the same issue as has been reported for 5 years regarding the ListView.

can someone help me i have this same problem.Thanks

Was this page helpful?
0 / 5 - 0 ratings