Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 757
CarmeloS Avatar asked Dec 10 '12 01:12

CarmeloS


People also ask

What is main thread in iOS?

The main thread is the one that starts our program, and it's also the one where all our UI work must happen. However, there is also a main queue, and although sometimes we use the terms “main thread” and “main queue” interchangeably, they aren't quite the same thing.

Why update UI on main thread iOS?

And the fact that UIKit is tied to the main thread makes it very easy to write concurrent programs and use UIKit. All you have to do is make sure that calls into UIKit are always made on the main thread. Now assume we use magic to refactor the UIKit, this magical UIKit can perfectly solve the problems above.

What is main thread checker?

The Main Thread Checker (MTC) is a runtime tool that throws a warning when system API calls that should be made on the main thread, such as UI operations, are incorrectly called on a background thread.

What is threading in swift?

First a precursor, threading is all about managing how work is prioritized in your app. Making your code execute faster is great, but what matters more is how fast the user perceives your app to be. Your goal as a developer is to prioritize anything that the user can see and interact with.


1 Answers

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).

like image 106
Jesse Rusak Avatar answered Sep 22 '22 03:09

Jesse Rusak