Bug 43068 - Updating ObeservableCollection on background thread bound to ListView.ItemsSource throws exception on windows, not Android.
Summary: Updating ObeservableCollection on background thread bound to ListView.ItemsSo...
Status: RESOLVED ANSWERED
Alias: None
Product: Forms
Classification: Xamarin
Component: Forms ()
Version: 2.0.0
Hardware: PC Windows
: Normal major
Target Milestone: ---
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2016-08-04 19:44 UTC by joshua.sells
Modified: 2017-06-15 13:29 UTC (History)
5 users (show)

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

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 on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED ANSWERED

Description joshua.sells 2016-08-04 19:44:28 UTC
# Steps to reproduce
Create a PCL Xamarin.Forms sln. with Android and UWP/WinRT platforms.  Create a ViewModel that implements INotifyPropertyChanged with a an ObservableCollection property that gets changed from a background thread. Bind a ListView's ItemsSource to the ViewModel's property.

# Expected behavior
The UI should be updated when the background thread updates the ViewModel's ObservableCollection.  This *is* what happens on Android.

# Actual behavior
On Android: the ListView is updated.
On Windows: 
" System.NullReferenceException: Object reference not set to an instance of an object.
   at Xamarin.Forms.Platform.UWP.WindowsBasePlatformServices.get_IsInvokeRequired()
   at Xamarin.Forms.ListProxy.OnCollectionChanged(Object sender, NotifyCollectionChangedEventArgs e)
   at Xamarin.Forms.ListProxy.WeakNotifyProxy.OnCollectionChanged(Object sender, NotifyCollectionChangedEventArgs e)
   at System.Collections.ObjectModel.ObservableCollection`1.OnCollectionChanged(NotifyCollectionChangedEventArgs e)
   at System.Collections.ObjectModel.ObservableCollection`1.InsertItem(Int32 index, T item)
   at System.Collections.ObjectModel.Collection`1.Add(T item)
   at XamarinUWPdataBindBug.Page1ViewModel.<<-ctor>b__4_0>d.MoveNext()"

# Supplemental info (logs, images, videos)
Example code:
https://github.com/jasells/XamarinUWPdataBindBug/tree/master 

I was actually trying to repro a *different* bug related to ObservableCollections bound to a View, but found this one.  I will continue to try to repro my original bug, as this may be related.

Using Xamarin.Forms.Device.BeginInvokeOnMainThread to modify the ObservableCollection can avoid this issue, but is not intuitive in a PCL that works on Android (iOS is untested).  Is Android the outlier, or is Windows?  Should be consistent across all platforms, and I believe should be handled internally to Xamarin.Forms databinding framework.

# Test environment (full version information)
Windows 10 (desktop), Visual Studio 2015 U3, Xamarin.Forms v. 1.5 or newer, occurs in both Windows 8.1 and UWP apps run on Windows 10.
Comment 1 joshua.sells 2016-08-04 19:47:51 UTC
To be clear, all Windows Apps were run on the *desktop* Windows OS, not on the phone OS (any version), but is assumed to be present.
Comment 2 Paul DiPietro [MSFT] 2017-06-15 13:29:45 UTC
This issue is going to be closed because it is not specifically a Forms issue. Per the MSDN docs[1], ObservableCollection<T> is not guaranteed to be thread safe, and as such an instance of it should not be modified on a background thread when bound to something in the UI. The fact that the scenario here appears to work on Android or iOS is fortunate, but not intended on our part, and could very well break at some point in the future.

[1] https://msdn.microsoft.com/en-us/library/ms668604(v=vs.110).aspx#Anchor_10