4

I know that drawLayer: and drawlayer:inContext: are called on multiple threads when using a CATiledlayer, but what about drawRect:?

Apple's PhotoScroller example code uses drawRect: to get its images from disk, and it has no special code for handling threads.

I am trying to determine whether my model for a CATiledLayer must be thread-safe.

kubi
  • 48,104
  • 19
  • 94
  • 118
Steve Weller
  • 4,599
  • 4
  • 23
  • 30

3 Answers3

2

Yes, drawRect can and will be called on multiple threads (tested on OS 4.2).

This behaviour is less obvious if your drawing is fast enough to outpace the arrival of new zoom gestures so your app may work fine until tested with rapid input of zoom gestures.

One alternative is to make your model thread-safe.

If thread-safety is achieved by synchronizing most of the access to the data model to one drawing thread at a time then then you might do just as well to mutex the body of drawRect with something like @syncrhonize(self) which seems to work.

I haven't found a way to request that CATiledLayer only uses one background thread.

persiflage
  • 1,154
  • 12
  • 22
  • You could also consider not assigning the model to a view object (your layer) directly. But instead having your controller handle the model changes and only assign immutable thread-safe objects to the layer. If the model changes you can always update the layer and call setNeedsDisplay. – Alej Nov 15 '11 at 22:23
2

I have found CATiledLayer is using multiple background threads in the iOS Simulator, but a single background thread on my iPhone.

My Mac has a dual core processor, while my iPhone has a single core (A4).

I suspect an iOS device with an A5 CPU will also use multiple threads.

Abhi Beckert
  • 32,787
  • 12
  • 83
  • 110
1

Have you seen this technical Q&A from Apple?

It doesn't answer your question directly, but it could help you decide how to implement your model.

Matt Long
  • 24,438
  • 4
  • 73
  • 99
  • That article contains a contradiction, but it pretty much says you have to be threaded. Watching the WWDC session video gives me one more piece of the puzzle that convinces me that this is the case. – Steve Weller Jun 19 '10 at 03:22