Bug 17445 - Missing [ThreadSafe] in MapKit
Summary: Missing [ThreadSafe] in MapKit
Status: VERIFIED FIXED
Alias: None
Product: iOS
Classification: Xamarin
Component: Xamarin.iOS.dll ()
Version: master
Hardware: PC Mac OS
: --- normal
Target Milestone: 7.2.1
Assignee: Sebastien Pouliot
URL:
Depends on:
Blocks:
 
Reported: 2014-01-27 14:37 UTC by Sebastien Pouliot
Modified: 2014-03-21 10:54 UTC (History)
3 users (show)

Tags:
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:
VERIFIED FIXED

Description Sebastien Pouliot 2014-01-27 14:37:40 UTC
By default MapKit gets the UI thread check enabled - but it looks like we're missing some [ThreadSafe] attributes inside it.

ref: maccore / 2b8c626743ecc4e238e7953117c27a66ab026842
Comment 1 Sebastien Pouliot 2014-01-27 15:34:09 UTC
Original report: http://forums.xamarin.com/discussion/12782/mkpolyline-fromcoordinates-and-ui-threads#latest

Documentation about thread safety is not easy to find (and not always accurate). According the [1] modifying `overlays` must be done on the main thread - but comparing them (which I assume means use them) "may occur on a different thread".

That would seem to make thread-safe all types that conforms to MKOverlay: e.g. MKCircle, MKPolygon, MKPolyline, MKTileOverlay

[1] https://developer.apple.com/library/ios/documentation/userexperience/conceptual/LocationAwarenessPG/AnnotatingMaps/AnnotatingMaps.html
Comment 2 Sebastien Pouliot 2014-01-28 12:03:26 UTC
Fixed in master / 4af06c0daec6e9aec079a0f4ab92204bb6dbf542
Comment 3 Ram Chandra 2014-03-21 09:53:55 UTC
I have checked this issue on following builds:

Mac
Xamarin Studio : 4.2.4 (build 32)
Xamarin.iOS : 7.2.1.22 (Trial Edition)
Build Information
Release ID: 402040032
Git revision: a160c35dac9ab9fd32eeadaa171216316d5a5133
Build date: 2014-03-12 13:55:08-04
Xamarin addins: a779416ceabd54981ce812771a4061c942e8b872

I observe when I set "UIApplication.CheckForIllegalCrossThreadCalls to true" in "AppDelegate.cs" class and  call the "MKPolyline.FromCoordinates ()" from background thread it doesn't throw any exception and sample app (MapDemo) is working fine. Code used to call "MKPolyline.FromCoordinates ()" is as follows

              public override void ViewDidLoad ()
		{
			base.ViewDidLoad ();
			// set map type and show user location
			map.MapType = MKMapType.Standard;
			map.ShowsUserLocation = true;
			CLLocationCoordinate2D[] poly = new CLLocationCoordinate2D[]{
				new CLLocationCoordinate2D(10.74812, -97.330277),
				new CLLocationCoordinate2D(10.74501, -97.350277),
				new CLLocationCoordinate2D(10.74912, -97.340277),
			};
			MKPolyline line=new MKPolyline();

			Thread t2 = new Thread(delegate()
				{
			 line = MKPolyline.FromCoordinates (poly);
				});
			t2.Start();
			t2.IsBackground = true;
			map.AddOverlay(line);
		}

screencast:  http://www.screencast.com/t/xkJZ69dQ

This issue is working fine now we are not getting any exception when we call "MKPolyline.FromCoordinates ()" from background thread. As of now I am closing this issue. 

I also observed that when I put the "map.AddOverlay(line);" code inside the thread "t2" it throws the 
the exception "MonoTouch.UIKitThreadAccessException" and the exception message is "MonoTouch.UIKit.UIKitThreadAccessException: UIKit Consistency error: you are calling a UIKit method that can only be invoked from the UI thread". 

Please let me know if I have to file different issue for this.
Comment 4 Ram Chandra 2014-03-21 10:24:12 UTC
An Update to comment3. Please ignore the code used for calling MKPolyline.FromCoordinates()".

The  code used to call "MKPolyline.FromCoordinates()" is as follows:

             public override void ViewDidLoad ()
		{
			base.ViewDidLoad ();
			// set map type and show user location
			map.MapType = MKMapType.Standard;
			map.ShowsUserLocation = true;
			CLLocationCoordinate2D[] poly = new CLLocationCoordinate2D[]{
				new CLLocationCoordinate2D(10.74812, -97.330277),
				new CLLocationCoordinate2D(10.74501, -97.350277),
				new CLLocationCoordinate2D(10.74912, -97.340277),
			};
			MKPolyline line=new MKPolyline();

			Thread t2 = new Thread(delegate()
				{
					line = MKPolyline.FromCoordinates (poly);
					//map.AddOverlay(line);
				});
			t2.IsBackground = false;
			t2.Start();
			map.AddOverlay(line);
		}
Comment 5 Sebastien Pouliot 2014-03-21 10:54:25 UTC
> Please let me know if I have to file different issue for this.

No, like stated in comment #1:

>> According the [1] modifying `overlays` must be done on the main thread