Skip to content

Instantly share code, notes, and snippets.

@vitonzhangtt
Last active August 20, 2018 15:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vitonzhangtt/3efd56e11e8a12370a78abd06ea489dd to your computer and use it in GitHub Desktop.
Save vitonzhangtt/3efd56e11e8a12370a78abd06ea489dd to your computer and use it in GitHub Desktop.
How to use GCD group?
- (void)test_group {
dispatch_queue_t queue = dispatch_queue_create("diamond-queue", DISPATCH_QUEUE_CONCURRENT);
dispatch_group_t group = dispatch_group_create();
for (int i = 0; i < 10; i++) {
// dispatch_group_enter(group);
dispatch_sync(queue, ^{
dispatch_group_enter(group);
NSLog(@"i: [%@]__LINE__: %@", @(i), @(__LINE__));
[self do_async_operation:^{
// Main Thread
NSLog(@"In do_async_operation");
dispatch_group_leave(group);
}];
});
}
dispatch_group_notify(group, queue, ^{
NSLog(@"__LINE__: %@", @(__LINE__));
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"__LINE__: %@", @(__LINE__));
});
});
NSLog(@"__LINE__: %@", @(__LINE__));
}
- (void)do_async_operation:(void(^)(void))block {
//
dispatch_async(dispatch_get_global_queue(QOS_CLASS_BACKGROUND, 0), ^{
sleep(2);
dispatch_async(dispatch_get_main_queue(), ^{
// Main Thread
block();
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment