views:

31

answers:

1

Are there any howtos?

What is the best practice here for background thread drawing.

Is it okay to store the rectangle data from [NSView drawRect] in a queue and let the background thread take the rectangle and create some bitmap and render the data into the bitmap and then use performSelectorOnMainThread:withObject to draw it? Or can i directly draw into the a context from the background.

I bought the book "Programming with Quartz 2D" from Bunny Laden but haven't read it yet and there is no hint about multithreading in the book. Also couldn't find anything in the normal Apple API Reference pages.

+1  A: 

Yes it's ok to store the rectangle data from [NSView drawRect] in a queue and let the background thread take the rectangle and create some bitmap and render the data into the bitmap and then use performSelectorOnMainThread:withObject to draw it.

As long as you do it in a thread safe manner.

That has nothing to do with drawing, so there is no reason it would be mentioned in "Programming with Quartz 2D" (which is a great book by the way - you should definitely get round to reading it). You probably want a companion book on multithreading.

Just consider the first part of your question. How are you going to store the rect in a queue? Add it to an NSMutableArray? Not thread safe.

Grand Central Dispatch is going to help a lot (you don't mention what platform you wish to support).

mustISignUp
Well i prefer posix pthreads for all sync stuff. But my question was if the NSFont, NSColor, NSImage or the Quartz counterparts are all thread safe - at least when if i garantee that my code is only calling one object from one thread at a time. If might pass objects from one thread to another, so they can't use globals or thread locals.
Lothar
Being thread safe means you don't have to guarantee use from only one thread at a time. If you can guarantee that then problem solved. Some gui classes have to be used on the main thread - this is nothing to do with thread safety - this is because of runloops, notificatons and such. NSImage may fall into this category. Stick with quartz and coretext.
mustISignUp