Bug 45373 - MKPolygonRenderer expects UI thread when accessing Polygon property
Summary: MKPolygonRenderer expects UI thread when accessing Polygon property
Alias: None
Product: iOS
Classification: Xamarin
Component: Xamarin.iOS.dll ()
Version: XI 10.0 (iOS10)
Hardware: Macintosh Mac OS
: --- normal
Target Milestone: Untriaged
Assignee: Bugzilla
Depends on:
Reported: 2016-10-13 17:20 UTC by Mario Rodriguez
Modified: 2016-10-18 02:14 UTC (History)
2 users (show)

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:

Description Mario Rodriguez 2016-10-13 17:20:15 UTC
I have subclass of MKPolygonRenderer which is overriding the DrawMapRect method.  I'm trying to retrieve the Polygon's Title property, but any attempt to access either the Polygon or Overlay properties throws the following exception:

Exception thrown: UIKit.UIKitThreadAccessException: UIKit Consistency error: you are calling a UIKit method that can only be invoked from the UI thread.
  at UIKit.UIApplication.EnsureUIThread () [0x00023] in /Users/builder/data/lanes/3818/ad1cd42d/source/xamarin-macios/src/UIKit/UIApplication.cs:88 
  at MapKit.MKPolygonRenderer.get_Polygon () [0x00000] in /Users/builder/data/lanes/3818/ad1cd42d/source/xamarin-macios/src/build/ios/native/MapKit/MKPolygonRenderer.g.cs:92 

I have verified that doing the same thing using Swift and XCode does not result in an error.  Apple's doc makes no mention of the need for the UI thread when accessing the Polygon property.
Comment 1 Mario Rodriguez 2016-10-14 21:04:54 UTC
This issue seems to be a bit more pervasive than just the MKPolygonRenderer class.  It also occurs with the MKPolylineRenderer's PolyLine property.

Others that I've found:
* MKPolygon.Title and MKPolyline.Title
* NSString's GetSizeUsingAttributes and DrawString(CGPoint, UIStringAttributes)

In all these cases, using P/Invoke to call the underlying ObjC selectors works without issue.

It makes it difficult to write custom overlay renderers, since their drawing typically does not occur on the UI thread.
Comment 2 Alex Soto [MSFT] 2016-10-18 02:14:20 UTC

This is a DEBUG only enabled feature that exists to aid developers that could not be aware that they are making UIKit calls from a background thread, we have opted to be conservative and mark as Thread Safe what is documented, but it is very possible that there are some APIs that are thread safe and just have not been documented as such. OTOH you just might have been very lucky to not crash by not calling these APIs from the main thread.

You can disable the feature for debug builds by passing the --disable-thread-check flag to the additional mtouch arguments inside your build options in project settings, or you can do this at runtime by changing the value of UIApplication.CheckForIllegalCrossThreadCalls, like this:

// Disable UIKit thread checks for a couple of methods
var previous = UIApplication.CheckForIllegalCrossThreadCalls;
UIApplication.CheckForIllegalCrossThreadCall = false;

// Perform some UIKit calls here
MKPolygon.Title = "Hello";

// Restore
UIApplication.CheckForIllegalCrossThreadCalls = previous;

Feel free to reopen the bug report if you need further assistance!