I have few codes like below:
- (void)viewDidLoad {
[super viewDidLoad];
NSLog(@"1");
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"3");
});
NSLog(@"2");
}
In console, the console output are: 1, 2, 3
. At first I thought it should be 1, 3, 2
cause dispatch_get_main_queue
is the main queue, and outside it is main queue as well. Why the output is different?
you dispatch the code in NSLog(3) using dispatch_async which will make it be executed asynchronously after the the current synchronous function is done.
I'd recommend reading a tutorial on Threading in General and GCD in particular since SO cant&shouldnt fully cover this.