-(void) viewdidload {[Super viewdidload]; NSLog (@ "1"); Dispatch_sync (Dispatch_get_main_queue (), ^{NSLog (@ "2"); }); NSLog (@ "3");} What will it output and whyAnswer:Output 1,Use Dispatch_sync (Dispatch_get_main_queue () ^ () {block body}) in iOS;
Dispath adds a synchronized block to the home team;
At this point the home queue is waiting for Dispatch_sync (Dispatch_get_main_queue (), ^ () {block Body}), and the execution
The Dispatch_sync is waiting for the home queue to finish executing.
thereby causing a deadlock.
So you should use asynchronous execution when iOS uses the primary queue Dispatch_get_main_queue ()
Dispatch_Async(Dispatch_get_main_queue (), ^ () {
NSLog (@ "2");
});
Conclusion: Dispatch_sync
Will call the Dispatch_sync line routines and so on the contents of Dispatch_sync, and then continue to execute.
Dispatch_async
The thread that calls Dispatch_async does not have to wait for dispatch_async content to continue executing itself.
Dispatch_sync/async (Dispatch_get_main_queue (), ^ () {Block Body})
Just add the block to the queue. Sequence of execution in order of queue
The difference between sync/async is whether the thread that calls Diapatch waits for dispatch to finish executing.
Thread Deadlock Issues