My favorite iOS interview question
It's a question I like to ask a lot on interviews.
Please tell, what are the possible orderings of the digits logged onto the console in every case.
Case 1
-(void)f { //runs on main thread
NSLog(@"1");
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"2");
});
NSLog(@"3");
}
Case 2
-(void)f { //runs on main thread
NSLog(@"1");
dispatch_sync(dispatch_get_main_queue(), ^{
NSLog(@"2");
});
NSLog(@"3");
}
Case 3
-(void)f { //runs on main thread
NSLog(@"1");
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
NSLog(@"2");
});
NSLog(@"3");
}
Case 4
-(void)f { //runs on main thread
NSLog(@"1");
dispatch_sync(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
NSLog(@"2");
});
NSLog(@"3");
}
Find out for yourself!