Bug 45182 - IndexOutOfRangeException when attempting to update an ObservableCollection bound to a ListView within a background thread.
Summary: IndexOutOfRangeException when attempting to update an ObservableCollection bo...
Status: CONFIRMED
Alias: None
Product: Forms
Classification: Xamarin
Component: Forms ()
Version: 2.3.2
Hardware: PC Windows
: --- normal
Target Milestone: ---
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2016-10-07 16:13 UTC by Ammar Mheir
Modified: 2017-08-04 17:03 UTC (History)
7 users (show)

Tags: ac
Is this bug a regression?: ---
Last known good build:


Attachments
Test Case (61.31 KB, application/x-zip-compressed)
2016-10-07 16:13 UTC, Ammar Mheir
Details


Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report for Bug 45182 on Developer Community or GitHub if you have new information to add and do not yet see a matching new report.

If the latest results still closely match this report, you can use the original description:

  • Export the original title and description: Developer Community HTML or GitHub Markdown
  • Copy the title and description into the new report. Adjust them to be up-to-date if needed.
  • Add your new information.

In special cases on GitHub you might also want the comments: GitHub Markdown with public comments

Related Links:
Status:
CONFIRMED

Description Ammar Mheir 2016-10-07 16:13:58 UTC
Created attachment 17936 [details]
Test Case

## Overview

IndexOutOfRange errors when attempting to update an ObservableCollection bound to a ListView on a background thread. The error does not occur if the ObservableCollection was updated on the MainUI thread rather than in a background thread.

## Steps to reproduce 

1. Download Sample project
2. Build Solution -> Set Android or iOS as startup project
3. Run Application
4. Noticed how after a few refreshes, the app will eventually crash with IndexOutOfRange errors.
5. Changing the updating Task in TheService.cs to start on Main thread via Device.InvokeOnMainThread will not cause an error. 

## Expected Results

Xamarin.Forms application should be able to continuously update the ObservableCollection without running into IndexOutOfRange errors in the background thread. The app should be able to continue the LongRunning task.


## Actual Results

Xamarin.Forms will run into errors once sporadically once the ObservableCollection bound to the ListView it changed. The crashes look slightly different between devices but always reference IndexOutOfRange error when attempting to access the collection. 

Android error logs 6.0:

>10-07 01:37:17.213 F/art     ( 2004): art/runtime/java_vm_ext.cc:410] JNI DETECTED ERROR IN APPLICATION: JNI CallObjectMethodA called with pending exception java.lang.IndexOutOfBoundsException: Invalid index 1, size is 1
>10-07 01:37:17.213 F/art     ( 2004): art/runtime/java_vm_ext.cc:410]   at throwIndexOutOfBoundsException (ArrayList.java:255)
>10-07 01:37:17.213 F/art     ( 2004): art/runtime/java_vm_ext.cc:410]   at get (ArrayList.java:308)
>10-07 01:37:17.213 F/art     ( 2004): art/runtime/java_vm_ext.cc:410]   at getView >(HeaderViewListAdapter.java:225)
>10-07 01:37:17.213 F/art     ( 2004): art/runtime/java_vm_ext.cc:410]   at obtainView (AbsListView.java:2346)
>10-07 01:37:17.214 F/art     ( 2004): art/runtime/java_vm_ext.cc:410]   at makeAndAddView (ListView.java:1875)
>10-07 01:37:17.214 F/art     ( 2004): art/runtime/java_vm_ext.cc:410]   at fillDown (ListView.java:702)
>10-07 01:37:17.214 F/art     ( 2004): art/runtime/java_vm_ext.cc:410]   at fillSpecific (ListView.java:1366)

##  Version Information

Microsoft Visual Studio Enterprise 2015
Version 14.0.25425.01 Update 3
Microsoft .NET Framework
Version 4.6.01586

Xamarin.Forms 2.3.2.127

Xamarin   4.2.0.703 (3b7df6f)
Xamarin.Android   7.0.1.3 (96c7ba6)
Xamarin.iOS 10.0.1.10 (ad1cd42)
Comment 1 Chris King 2016-10-21 15:58:59 UTC
confirmed on android on master; after a few refreshes, app crashes with IndexOutOfRangeException
Comment 2 Chase Long 2017-08-04 17:03:06 UTC
My understanding is that the framework assumes bounds collections will only be modified on the UI thread. If that's true, then this is not a bug. Can anyone clarify whether modifying bound ObservableCollections on threads other than the UI thread is supposed to be supported?