Last active
August 20, 2018 15:08
-
-
Save vitonzhangtt/3efd56e11e8a12370a78abd06ea489dd to your computer and use it in GitHub Desktop.
How to use GCD group?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (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