Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is this Core Graphics code thread safe?

I know it's safe to draw on any thread so long as I call

UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.opaque, 0.0);
UIGraphicsEndImageContext();

on the same thread.

Taking a screenshot of a view via this method takes about 300 ms, which is not bad, but I'm in a tight situation, so I want to do it in a background thread.

Here's what I'm doing:

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.opaque, 0.0);
        [view.layer renderInContext:UIGraphicsGetCurrentContext()];
        UIImage * screenshot = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
    });

The only thing here in question is the view, which lies on the main thread. Is it safe to call renderInContext on a view.layer from a background thread? Or generally, is it safe to read-only of a UIKit object from another thread?

(And please don't give me the default "UIKit is not thread safe" answer. I already know that. This here is a special case (and don't tell me there are no special cases).)

(the code above works fine, but I'm not sure if that's just a coincidence.)

like image 298
Snowman Avatar asked Jul 06 '12 04:07

Snowman


People also ask

Is Core Data thread-safe or not?

Core Data is designed to work in a multithreaded environment. However, not every object under the Core Data framework is thread safe. To use Core Data in a multithreaded environment, ensure that: Managed object contexts are bound to the thread (queue) that they are associated with upon initialization.

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 Nsdictionary thread-safe?

In general, the collection classes (for example, NSMutableArray , NSMutableDictionary ) are not thread-safe when mutations are concerned. That is, if one or more threads are changing the same array, problems can occur.

What is thread-safe in iOS?

Thread Safety APIs in iOSBy creating a queue of tasks where only one task can be processed at any given time, you are indirectly introducing thread safety to the component that is using the queue.


1 Answers

Core Graphics and Core Animation being low-level APIs, are generally thread safe. However, the same rules about access still apply: Any work must not be accessed by more than one thread at the same time, else drawing will fail and your app will crash. I would be wary (but not afraid) of UIImage, as UIKit objects aren't just not thread safe, they're basically ticking time bombs in background threads, and will happily dive straight off a cliff into Exception Land for no good reason. However, because UIImage is just a CGImage wrapper, again, most drawing is thread safe.

like image 73
CodaFi Avatar answered Oct 19 '22 18:10

CodaFi