Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Thread-safe UIKit methods

Tags:

ios

uikit

I'm trying to find out exactly what methods (names!) became thread safe in UIKit on iOS 4.0.

I've searched through Apple's docs with little success. For instance a UIImage created with imageNamed: is not safe to use on other threads (it will occasionally jettison its CGImage from the main thread), while imageWithCGImage might be(?). Also, I've seen claims that UIColor is safe to use on threads other than the main thread.

Is there some definite guide as to which methods are safe which ones isn't?

Edit: What's interesting is UIKit classes that is interesting to use on separate threads, such as UIImage, UIColor etc.

like image 254
Nuoji Avatar asked May 22 '11 09:05

Nuoji


People also ask

Is UIKit thread-safe?

As we can see, most of the components in UIKit is described as nonatomic `, this means there are not thread safe.

Is NSArray thread-safe?

In general, immutable classes like NSArray are thread-safe, while their mutable variants like NSMutableArray are not. In fact, it's fine to use them from different threads, as long as access is serialized within a queue.

Is UIImage thread-safe?

Loading even a medium-sized image on the main thread will likely take longer than 16.6 milliseconds. Naturally, people identify this as an issue and move image creation to background threads. Generally, the code is based on the assumption that -[UIImage imageWithData:] and -[UIImage imageWithCGImage:] are thread safe.

Is iOS single thread?

Each process (application) in OS X or iOS is made up of one or more threads, each of which represents a single path of execution through the application's code. Every application starts with a single thread, which runs the application's main function.


Video Answer


1 Answers

From Apple's documentation:

Note: For the most part, UIKit classes should be used only from an application’s main thread. This is particularly true for classes derived from UIResponder or that involve manipulating your application’s user interface in any way.

Therefore, you really shouldn't be interacting with anything in UIKit on a background thread.

like image 146
Jacob Relkin Avatar answered Oct 05 '22 14:10

Jacob Relkin