Search code examples
iphoneiosuiapplication

iOS: is calling methods of UIApplication in thread other than the main thread safe?


Some say that one must use UIApplication in main thread, but I don't see this documented in Apple's doc. So can someone please tell me if it is safe to use UIApplication in a separate thread? If it is not safe, where is this documented?

EDIT: I'm focusing on those methods which manipulate local notifications.


Solution

  • Most UIKit classes are not thread safe. This has been discussed at length at WWDC, etc, but it is not very well documented. The best I could find is TN2109 which repeatedly discusses how calling UIKit from a secondary thread is not permitted.

    Note that there are many documented exceptions to this rule. For example, the beginBackgroundTaskWithExpirationHandler and related methods on UIApplication are clearly labeled as safe to call from non-main threads. (This also implies that the other methods of UIApplication are not safe to call from other threads, since these are specifically called out.)

    There are other exceptions to this rule as well, mostly involving drawing (UIImage, and UIColor instances are thread-safe, as of iOS 4, at least).