Bug 16707 - Overzealous UIKitThreadAccessException for UILocalNotification
Summary: Overzealous UIKitThreadAccessException for UILocalNotification
Alias: None
Product: iOS
Classification: Xamarin
Component: General ()
Version: 7.0.4.x
Hardware: Macintosh Mac OS
: High normal
Target Milestone: Untriaged
Assignee: Bugzilla
Depends on:
Reported: 2013-12-10 13:49 UTC by chris
Modified: 2013-12-12 06:11 UTC (History)
3 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 chris 2013-12-10 13:49:21 UTC
I am getting a `MonoTouch.UIKit.UIKitThreadAccessException` when doing `new UILocalNotification()` from a background thread.

I don't think that's valid because Apple's docs state:

Applications that run in the allowed period in the background may also present a local notification, scheduled or immediate, to inform the user of an incoming message, chat, or update. An application can have only a limited number of scheduled notifications; the system keeps the soonest-firing 64 notifications (with automatically rescheduled notifications counting as a single notification) and discards the rest
Comment 1 chris 2013-12-11 02:08:27 UTC
Comment 2 Rolf Bjarne Kvinge [MSFT] 2013-12-11 18:12:56 UTC
When Apple's doc mentions "in the background," it doesn't refer to background threads, but instead the entire app running as a background app.

Additionally the documentation doesn't mention anything about thread-safety, and Apple's general recommendation about UIKit is: "For the most part, UIKit classes should be used only from an application’s main thread. [1]"

And there also seem to be anecdotal evidence that it may crash too: http://stackoverflow.com/a/8061520/183422

So I believe the exception is in fact correct (please feel free reopen this bug report if you encounter any documentation to the contrary though)

If you really want to try and see how it goes, you can disable the thread-check:

UIApplication.CheckForIllegalCrossThreadCall = false;
var notification = new UILocalNotification ();
UIApplication.CheckForIllegalCrossThreadCall = true;

[1] https://developer.apple.com/library/ios/documentation/uikit/reference/UIKit_Framework/Introduction/Introduction.html at the very bottom.
Comment 3 chris 2013-12-11 18:38:06 UTC
I was referring to a thread that's running when the app is in the background.

Please correct me if I am wrong but an app running in the background doesn't have a main/UI thread (at least it's paused). You can only create a new thread and do your work from there.

When setting UIApplication.CheckForIllegalCrossThreadCall = false; the code works fine and local notifications are displayed as they should when my app runs in the background.
Comment 4 Rolf Bjarne Kvinge [MSFT] 2013-12-11 19:03:48 UTC
There is nothing special thread-wise when your app is executing in the background - it's just that there's another app controlling the screen.

You still have your main/UI thread, but it's just not updating any UI. You can do work there if you wish.
Comment 5 chris 2013-12-12 05:38:08 UTC
Can you explain how this is done?

Your own sample uses a separate thread:


When I try to run it via UIApplication.SharedApplication.BeginInvokeOnMainThread() my code won't get called.
Comment 6 chris 2013-12-12 06:11:46 UTC
Sorry, that was a debugger issue.

Running with UIApplication.SharedApplication.BeginInvokeOnMainThread() works without having to do UIApplication.CheckForIllegalCrossThreadCall = false;