介绍一下主要的函数
1、dispatch_group_async
//将block加入到queue中,并和group相关联
void dispatch_group_async(dispatch_group_t group,
dispatch_queue_t queue,
dispatch_block_t block);
如果提交到dispatch_queue_t queue中与group关联的任务全都执行完毕会调用dispatch_group_notify并且dispatch_group_wait会停止等待;
2、dispatch_group_enter & dispatch_group_leave
//将代码块加入到group中,与dispatch_group_async功能类似
//开始一个任务
void dispatch_group_enter(dispatch_group_t group);
//结束的任务
void dispatch_group_leave(dispatch_group_t group);
当调用enter时计数加1,调用leave时计数减1,当计数为0时会调用dispatch_group_notify并且dispatch_group_wait会停止等待;
注意点: dispatch_group_enter 和 dispatch_group_leave 必须成对出现
3、dispatch_group_wait
//阻塞当前线程(不能放在主线程)等待group任务的完成,可以设置超时时间
long dispatch_group_wait(dispatch_group_t group,
dispatch_time_t timeout);
4 、dispatch_group_notify
/* dispatch_group_notify会把block加入到queue中,
* 当dispatch_group_t中的任务执行完成时执行
* 或者在dispatch_group_enter和dispatch_group_leave存在的情况下,计数为0的时候执行
*/
void dispatch_group_notify(dispatch_group_t group,
dispatch_queue_t queue,
dispatch_block_t block);
示例1 dispatch_group的简单使用:
dispatch_queue_t queue_t = dispatch_queue_create("gcd_demo_queue", DISPATCH_QUEUE_CONCURRENT);
dispatch_group_t group_t = dispatch_group_create();
dispatch_group_async(group_t, queue_t, ^{
sleep(2);
NSLog(@"任务1完成");
});
dispatch_group_async(group_t, queue_t, ^{
sleep(3);
NSLog(@"任务2完成");
});
dispatch_group_async(group_t, queue_t, ^{
NSLog(@"任务3完成");
});
//任务完成方式一
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
dispatch_group_wait(group_t, DISPATCH_TIME_FOREVER);
NSLog(@"任务完成通知 Wait");
});
//任务完成方式二
dispatch_group_notify(group_t, dispatch_get_main_queue(), ^{
NSLog(@"任务完成通知 Notify");
});
输出结果
2018-01-04 14:10:15.884470+0800 GCD_Demo[2869:439062] 任务3完成
2018-01-04 14:10:17.886704+0800 GCD_Demo[2869:439063] 任务1完成
2018-01-04 14:10:18.887726+0800 GCD_Demo[2869:439061] 任务2完成
2018-01-04 14:10:18.887978+0800 GCD_Demo[2869:439060] 任务完成通知 Wait
2018-01-04 14:10:18.887988+0800 GCD_Demo[2869:439018] 任务完成通知 Notify
需要注意的是dispatch_group_wait会阻塞当前线程,所以不能放到主线程
示例2 dispatch_group 多个queue的使用:
dispatch_queue_t queue_t = dispatch_queue_create("gcd_demo_queue", DISPATCH_QUEUE_CONCURRENT);
dispatch_queue_t queue_global_t = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_group_t group_t = dispatch_group_create();
dispatch_group_async(group_t, queue_t, ^{
sleep(2);
NSLog(@"任务1完成");
});
dispatch_group_async(group_t, queue_t, ^{
sleep(3);
NSLog(@"任务2完成");
});
//任务3 在系统的线程中执行
dispatch_group_async(group_t, queue_global_t, ^{
sleep(5);
NSLog(@"任务3完成");
});
dispatch_group_notify(group_t, dispatch_get_main_queue(), ^(){
NSLog(@"notify:任务都完成了");
});
输出结果
2018-01-04 14:24:47.559384+0800 GCD_Demo[2998:488861] 任务1完成
2018-01-04 14:24:48.557911+0800 GCD_Demo[2998:488863] 任务2完成
2018-01-04 14:24:50.556268+0800 GCD_Demo[2998:488864] 任务3完成
2018-01-04 14:24:50.556476+0800 GCD_Demo[2998:488756] 任务完成通知
示例3 dispatch_group - dispatch_group_enter & dispatch_group_leave的使用:
dispatch_queue_t queue_t = dispatch_queue_create("gcd_demo_queue", DISPATCH_QUEUE_CONCURRENT);
dispatch_group_t group_t = dispatch_group_create();
dispatch_group_enter(group_t);
dispatch_async(queue_t, ^{
sleep(5);
NSLog(@"网络任务1完成");
dispatch_group_leave(group_t);
});
dispatch_group_enter(group_t);
dispatch_async(queue_t, ^{
sleep(3);
NSLog(@"网络任务2完成");
dispatch_group_leave(group_t);
});
dispatch_group_enter(group_t);
dispatch_async(queue_t, ^{
sleep(4);
NSLog(@"网络任务3完成");
dispatch_group_leave(group_t);
});
dispatch_group_notify(group_t, dispatch_get_main_queue(), ^{
NSLog(@"任务完成通知");
});
输出结果
2018-01-04 14:33:10.374991+0800 GCD_Demo[3110:527082] 网络任务2完成
2018-01-04 14:33:11.374908+0800 GCD_Demo[3110:527095] 网络任务3完成
2018-01-04 14:33:12.375411+0800 GCD_Demo[3110:526266] 网络任务1完成
2018-01-04 14:33:12.375620+0800 GCD_Demo[3110:526111] 任务完成通知