user859375
user859375

Reputation: 3699

CALayer.contents drawn in UI Thread?

Can you confirm that setting CALayer.contents property with CGImageRef in background thread still makes the core animation draw the contents image in main thread loop(which is UI Thread) rather than core animation thread or custom background thread which sets the contents property?

The reason I am asking this is that the core animation runs its own thread but however, it appears that when you set the CALayer.contents property the UI thread does the drawing to layer?

Upvotes: 2

Views: 2465

Answers (1)

gaige
gaige

Reputation: 17471

My experience is that if you have a visible layer and set the contents in a background thread, it is likely that will not draw immediately. The solution I used was to set the contents property asynchronously using a call to dispatch_async() on the main thread:

dispatch_async(dispatch_get_main_queue(), ^(void) {
    layer.contents = (id)myCGImage;
});

In particular, my example here is for working with threads using GCD, where the thread itself may long outlive the operation that you are running. In this case, placing the assignment on the main thread, or forcing a [CATransaction flush] are two ways to indicate to the OS that you want that data to be presented to the user.

Otherwise, in the case of a GCD background thread, you are at the mercy of the thread's run loop which may not execute the [CATransaction flush] for a long time (in my experience, seconds).

Upvotes: 2

Related Questions