dispatch_sync() lockup workaround
dispatch_sync() lockup workaround
- Subject: dispatch_sync() lockup workaround
- From: Abhi Beckert <email@hidden>
- Date: Wed, 20 Oct 2010 11:15:55 +1000
Hi,
According to the documentation dispatch_sync(dispatch_get_main_queue(), ^{ ... }) will lockup if you are already on the main queue.
I have several methods which can be called from either a background queue or the main queue, but parts of them *must* be run on the main queue. What is a clean workaround for this? I'd like to do something like:
- (void)foo
{
dispatch_block_t fooBlock = ^{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
/* do stuff */
[pool release];
};
if (dispatch_get_current_queue() == dispatch_get_main_queue())
dispatch_exec_block(fooBlock);
else
dispatch_sync(dispatch_get_main_queue(), fooBlock);
}
I made up dispatch_exec_block(), it must exist, but I can't find it?
Also, should I be using a higher level API? Perhaps NSBlockOperation? I'm not sure how to run an NSBlockOperation on the main queue. And I've noticed dispatch_sync() doesn't catch NSExceptions, and I'd really like to have the autorelease pool created automatically for me (just to eliminate typo-related bugs).
- Abhi_______________________________________________
Cocoa-dev mailing list (email@hidden)
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:
This email sent to email@hidden