Reputation:
If a run a synch block with the function : dispatch_sync
using the queue retrieved from dispatch_get_main_queue()
the application hang and the block is not executed, while if I pass the queue obtained from : dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
it works without any problem .
dispatch_queue_t q;
q = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
//q = dispatch_get_main_queue(); //If uncommented the app hang
dispatch_sync(q , ^{
NSLog(@".");
});
Why using the queue obtained from dispatch_get_main_queue()
cause the app to hang and the block doesn't get executed ?
Upvotes: 3
Views: 1544
Reputation: 4946
In addition to Rob's answer, here are the docs for Grand Central Dispatch (programming guide, reference). It is a great set of libraries, and had made concurrency mostly painless, but it is not fool proof.
Upvotes: 0
Reputation: 437382
If you synchronously dispatch to the main queue from the main queue, it should freeze. You're asking it to dispatch a block of code to the main queue, but because it's synchronous (dispatch_sync
), you're asking it to freeze the current queue (the main queue) until the dispatched queue (also the main queue) responds to what you've just added to it! It obviously can't do that.
Either do dispatch_async
, or dispatch to a different queue!
Upvotes: 10