oxyplot / oxyplot-xamarin

OxyPlot for Xamarin.Mac and Xamarin.Forms
MIT License
64 stars 59 forks source link

UIKitThreadAccessException on iOS: you are calling a UIKit method that can only be invoked from the UI thread #126

Closed janusw closed 1 year ago

janusw commented 2 years ago

WIth the 2.0 beta nupkg from #125, I get the following exception on iOS:

UIKit.UIKitThreadAccessException: UIKit Consistency error: you are calling a UIKit method that can only be invoked from the UI thread.
  at UIKit.UIApplication.EnsureUIThread () [0x00020] in /Library/Frameworks/Xamarin.iOS.framework/Versions/16.0.0.72/src/Xamarin.iOS/UIKit/UIApplication.cs:110
  at UIKit.UIView.SetNeedsDisplay () [0x00000] in /Library/Frameworks/Xamarin.iOS.framework/Versions/16.0.0.72/src/Xamarin.iOS/UIKit/UIView.g.cs:1486
  at OxyPlot.Xamarin.iOS.PlotView.InvalidatePlot (System.Boolean updateData) [0x00011] in <e72a6e778018417290b9b954eb310282>:0
  at OxyPlot.Xamarin.iOS.PlotView.set_Model (OxyPlot.PlotModel value) [0x00035] in <e72a6e778018417290b9b954eb310282>:0
  at OxyPlot.Xamarin.Forms.Platform.iOS.PlotViewRenderer.OnElementPropertyChanged (System.Object sender, System.ComponentModel.PropertyChangedEventArgs e) [0x00041] in <d733efafd1794d319fff2e9b7e057f44>:0
  at at (wrapper delegate-invoke) <Module>.invoke_void_object_PropertyChangedEventArgs(object,System.ComponentModel.PropertyChangedEventArgs)
  at Xamarin.Forms.BindableObject.OnPropertyChanged (System.String propertyName) [0x00000] in D:\a\1\s\Xamarin.Forms.Core\BindableObject.cs:266
  at Xamarin.Forms.Element.OnPropertyChanged (System.String propertyName) [0x00000] in D:\a\1\s\Xamarin.Forms.Core\Element.cs:362
  at Xamarin.Forms.BindableObject.SetValueActual (Xamarin.Forms.BindableProperty property, Xamarin.Forms.BindableObject+BindablePropertyContext context, System.Object value, System.Boolean currentlyApplying, Xamarin.Forms.Internals.SetValueFlags attributes, System.Boolean silent) [0x00114] in D:\a\1\s\Xamarin.Forms.Core\BindableObject.cs:510
  at Xamarin.Forms.BindableObject.SetValueCore (Xamarin.Forms.BindableProperty property, System.Object value, Xamarin.Forms.Internals.SetValueFlags attributes, Xamarin.Forms.BindableObject+SetValuePrivateFlags privateAttributes) [0x00173] in D:\a\1\s\Xamarin.Forms.Core\BindableObject.cs:446
  at Xamarin.Forms.BindableObject.SetValue (Xamarin.Forms.BindableProperty property, System.Object value, System.Boolean fromStyle, System.Boolean checkAccess) [0x0004d] in D:\a\1\s\Xamarin.Forms.Core\BindableObject.cs:374
  at Xamarin.Forms.BindableObject.SetValue (Xamarin.Forms.BindableProperty property, System.Object value) [0x00000] in D:\a\1\s\Xamarin.Forms.Core\BindableObject.cs:349
  at OxyPlot.Xamarin.Forms.PlotView.set_Model (OxyPlot.PlotModel value) [0x00000] in D:\a\oxyplot-xamarin\oxyplot-xamarin\Source\OxyPlot.Xamarin.Forms\PlotView.cs:56

Creating a PlotView with a PlotModel works fine, but updating the PlotView by setting a new PlotModel produces the error above, which did not occur with version 1.1.0-unstable0011 (from nuget.org).

It can easily be fixed on the application side by wrapping the assignment of the PlotModel into Device.BeginInvokeOnMainThread, but the question is why it only occurs with the new version, and whether OxyPlot.Xamarin itself shouldn't make sure that the update is done on the main thread.

janusw commented 1 year ago

I'm closing this, since it can easily be avoided by performing the update of the PlotModel on the main thread.