Re: dispatch_sync(dispatch_get_main_queue() UI weirdness
Re: dispatch_sync(dispatch_get_main_queue() UI weirdness
- Subject: Re: dispatch_sync(dispatch_get_main_queue() UI weirdness
- From: Rainer Brockerhoff <email@hidden>
- Date: Sat, 06 Sep 2014 09:59:45 -0300
On 9/6/14, 0:22, email@hidden wrote:
> Date: Sat, 06 Sep 2014 06:18:41 +0800
> From: Roland King <email@hidden>
> To: Jens Alfke <email@hidden>
> Cc: Jonathan Guy <email@hidden>, email@hidden
> Subject: Re: dispatch_sync(dispatch_get_main_queue() UI weirdness
> Message-ID: <email@hidden>
>>
>> Why not? I assume Jonathan's -shouldIAccept: is running a nested
>> event loop for the modal panel, since it doesn't return till the
>> user dismisses it.
>>
>> (Whether it's a dispatch_sync or a dispatch_async doesn't matter;
>> the main thread will be blocked for as long as the block takes to
>> run.)
>>
>> I still think the problem is that you're blocking the
>> dispatch-queue's thread for a long time. Try pausing the app while
>> the dialog is up and looking at what the various threads are
>> doing.
Sidenote: on OS X the best way to run a UI-doing block would be to
define this function:
void RunBlockOnMainThread(^(void)block) {
CFRunLoopPerformBlock([[NSRunLoop mainRunLoop] getCFRunLoop],
kCFRunLoopCommonModes, block);
}
and call that whenever you want something to be performed on the main
thread. But never do modal stuff in that block.
So the original code would look like:
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
RunBlockOnMainThread (^{
// So my app is doing some background stuff
// and I need a file from the user so
// code blah blah code
RunBlockOnMainThread(^{
NSOpenPanel *op = [NSOpenPanel openPanel];
[op beginWithCompletionHandler:^{
// handle the open panel results here
}];
});
// resume code blah blah code <== probably move this into the
results block?
});
}
HTH,
--
Rainer Brockerhoff <email@hidden>
Belo Horizonte, Brazil
"In the affairs of others even fools are wise
In their own business even sages err."
http://brockerhoff.net/blog/
_______________________________________________
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