Bug 22112 - MFMailComposeViewController calls Finish event after presented on non-UI thread
Summary: MFMailComposeViewController calls Finish event after presented on non-UI thread
Alias: None
Product: iOS
Classification: Xamarin
Component: General ()
Version: master
Hardware: Macintosh Mac OS
: Normal normal
Target Milestone: Untriaged
Assignee: Bugzilla
Depends on:
Reported: 2014-08-14 12:04 UTC by Colby Williams
Modified: 2014-09-04 21:46 UTC (History)
4 users (show)

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

Sample Solution (7.78 MB, application/zip)
2014-08-14 12:04 UTC, Colby Williams

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 Colby Williams 2014-08-14 12:04:11 UTC
Created attachment 7692 [details]
Sample Solution

After Presenting MFMailComposeViewController, it hangs for a couple of seconds, then fires the Finished event OFF of the UI thread.

Sample project attached
Comment 1 Parmendra Kumar 2014-08-20 08:27:38 UTC
I have tried to reproduce this,I am unable to reproduce this issue.
Could you please give us some Log or Environment info ?.

Screencast: http://screencast.com/t/kZwmCBnLM

Please the screencast and Let us know if I am missing anything.

Environment info:

=== Xamarin Studio ===

Version 5.2.1 (build 1)
Installation UUID: 1a096c6f-0678-402e-89b2-a2c10f7e80e4
	Mono 3.6.0 ((no/f540f8a)
	GTK+ 2.24.23 (Raleigh theme)

	Package version: 306000039

=== Xamarin.Android ===

Version: 4.14.0 (Business Edition)
Android SDK: /Users/360_macmini/Desktop/android-sdk-macosx
	Supported Android versions:
		2.1   (API level 7)
		2.2   (API level 8)
		2.3   (API level 10)
		3.1   (API level 12)
		3.2   (API level 13)
		4.0   (API level 14)
		4.0.3 (API level 15)
		4.1   (API level 16)
		4.2   (API level 17)
		4.3   (API level 18)
		4.4   (API level 19)
Java SDK: /usr
java version "1.6.0_65"
Java(TM) SE Runtime Environment (build 1.6.0_65-b14-462-11M4609)
Java HotSpot(TM) 64-Bit Server VM (build 20.65-b04-462, mixed mode)

=== Apple Developer Tools ===

Xcode 5.1 (5084)
Build 5B130a

=== Xamarin.iOS ===

Version: (Business Edition)
Hash: 606f31a
Build date: 2014-08-01 15:27:48-0400

=== Xamarin.Mac ===

Version: (Business Edition)

=== Build Information ===

Release ID: 502010001
Git revision: d06832ce9807d6be24aca225457e8b37c7669f6f
Build date: 2014-08-07 12:10:47-04
Xamarin addins: 1de032531be4cecf2f39dbee3b87aac78204058c

=== Operating System ===

Mac OS X 10.9.4
Comment 2 Sebastien Pouliot 2014-09-04 21:46:29 UTC
That's normal, i.e. an iOS feature. There's nothing in Apple documentation [1] stating on which thread the selector `mailComposeController:didFinishWithResult:error:` (on which `Finished` event is based) will be called from. 

IOW you cannot assume you'll be called on the main (UI) thread of your application. I tried it and was called from a different thread (and got the UIKitThreadAccessException [2]). Since you can't assume this you need to either:

a. not use any API (most of UIKit) that is not thread safe [2]; or

b. make sure any such code is executed on the main thread [3], e.g.

	private void FinishedSendingEmail(object s, MFComposeResultEventArgs args)
		this.InvokeOnMainThread (() => {
			Console.WriteLine (args.Result.ToString ());
			_mailController.DismissViewController (true, null);

[1] https://developer.apple.com/library/prerelease/ios/documentation/MessageUI/Reference/MFMailComposeViewControllerDelegate_protocol/

[2] http://tirania.org/monomac/archive/2012/Sep-10.html

[3] http://iosapi.xamarin.com/?link=M%3aMonoTouch.Foundation.NSObject.InvokeOnMainThread(MonoTouch.Foundation.NSAction)