The following snippet of code.. which is super interesting, is based on this post. This allows to dispatch a block, on Main Thread, once two async operations are completed.
[cc lang=”objc”]
dispatch_group_t group = dispatch_group_create();
dispatch_group_async(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^ {
NSLog(@”Block1″);
[NSThread sleepForTimeInterval:5.0];
NSLog(@”Block1 End”);
});
dispatch_group_async(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^ {
NSLog(@”Block2″);
[NSThread sleepForTimeInterval:8.0];
NSLog(@”Block2 End”);
});
dispatch_group_notify(group, dispatch_get_main_queue(), ^ {
NSLog(@”Block3 :: %d”, [NSThread isMainThread]);
});
dispatch_release(group);
[/cc]