I'm rewriting for 10.7/ARC some code that (basically) trampolines status 
between threads to avoid affecting the UI from background threads. This time 
around I'm using blocks, and it turns out this *dramatically* simplifies the 
code, which is a good thing. However, I notice I have this pattern a lot:

>       void (^block) (void) = ^{…};
>       …
>       dispatch_async (dispatch_get_main_queue (), block);

There are a few variations of this. Sometimes it's not the main queue, and 
sometimes it's dispatch_sync. Usually, the block is just a line or two of code, 
such as setting a property.

The problem is that the documentation clearly states that exceptions must not 
try to escape across dispatch queue operation boundaries. AFAICT, this means 
that for every one of the tiny code block fragments I write, not only does my 
fragment need to be wrapped in a '@try' block, I must also deal with the 
exception by (say) logging its description before leaving the block. Every 
time. That seems like an awful lot of boilerplate code, and it makes the 
blocks-based approach very unpalatable.

Secondarily, I can find nothing in any GCD-related documentation that addresses 
the proper use of autorelease pools in dispatched blocks. I'm *guessing* that I 
need to set up my own. That's much less onerous that handling exceptions (a 
simple @autoreleasepool wrapper), and I guess there's no real downside in 
creating my own pool even if unnecessary, but I still wish I knew what I was 
*supposed* to do.

I thought using NSOperationQueue and NSBlockOperation might save me from having 
to code around the lower-level GCD API, but I see that as of 10.6 exceptions 
aren't allowed to escape NSOperation threads either, and they're not 
automatically handled at the operation's top level AFAICT.

What experiences have other people been having in this area? Have I missed 
something obvious, or is exception handling an unavoidable PITA? What are the 
rules for autorelease pools when using Cocoa code in GCD-dispatched blocks?

I also wonder if things change if the blocks are being executed on the main 
queue and hence the main thread. In that case, I would assume, there's the 
default exception-catching code in place outside any GCD block execution 
context, as well as the main autorelease pool. If I ignore the documented rules 
when queuing blocks to the main queue, does it just work properly without all 
that exception/autorelease glue code?



_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to