
我们是工程师 ——加油 !.jpg
- 为了方便观看,我将队列、和组的命名都加上了YG后缀
-(void)test
{
//获取全局并发队列
dispatch_queue_t queueYG = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
// 创建队列组
dispatch_group_t groupYG = dispatch_group_create();
// ----------往队列组中添加耗时操作 1. 2. 3.---------
dispatch_group_async(groupYG, queueYG, ^{
NSLog(@"线程:%@--加载图片1", [NSThread currentThread]);
});
dispatch_group_async(groupYG, queueYG, ^{
NSLog(@"线程:%@--加载图片2", [NSThread currentThread]);;
});
dispatch_group_async(groupYG, queueYG, ^{
NSLog(@"线程:%@--加载图片3", [NSThread currentThread]);
});
//-------------------- ------------------
// 当并发队列组中的任务执行完毕后才会执行这里的代码
dispatch_group_notify(groupYG, dispatch_get_main_queue(), ^{
NSLog(@"线程:%@--合并图片", [NSThread currentThread]);
});
}
当耗时操作是异步执行的工具,比如像AFNetWorking等框架,上边的方法就不灵了,这是我们可以使用下面的方法;
-(void)test
{
//获取全局并发队列
dispatch_queue_t queueYG = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
// 创建队列组
dispatch_group_t groupYG = dispatch_group_create();
// ----------往队列组中添加耗时操作 1. 2. 3.---------
dispatch_group_enter(groupYG);
dispatch_group_async(groupYG, queueYG, ^{
[MLNetWork postWithURLString:@"https://api.joint-think.com/api/home/searchlog" parameters:@{@"userid":@"2"} success:^(NSDictionary *dic) {
NSLog(@"111111");
dispatch_group_leave(groupYG);
} failure:^(NSError *error) { }];
});
dispatch_group_enter(groupYG);
dispatch_group_async(groupYG, queueYG, ^{
[MLNetWork postWithURLString:@"https://api.joint-think.com/api/home/searchlog" parameters:@{@"userid":@"2"} success:^(NSDictionary *dic) {
NSLog(@"22222");
dispatch_group_leave(groupYG);
} failure:^(NSError *error) { }];
});
dispatch_group_enter(groupYG);
dispatch_group_async(groupYG, queueYG, ^{
[MLNetWork postWithURLString:@"https://api.joint-think.com/api/home/searchlog" parameters:@{@"userid":@"2"} success:^(NSDictionary *dic) {
NSLog(@"333333");
dispatch_group_leave(groupYG);
} failure:^(NSError *error) { }];
});
// 当并发队列组中的任务执行完毕后才会执行这里的代码
dispatch_group_notify(groupYG, dispatch_get_main_queue(), ^{
NSLog(@"刷新UI");
});
}
网友评论