1、使用场景
1、异步执行多个耗时任务。
2、当多个耗时任务都执行完回到主线程执行任务。
2、dispatch_group_notify
监听 group 中任务的完成状态,当所有的任务都执行完成后,才执行dispatch_group_notify block 中的任务。
dispatch_group_t group = dispatch_group_create();
dispatch_queue_t queue = dispatch_queue_create("cwwng-queue", DISPATCH_QUEUE_CONCURRENT);
dispatch_group_async(group, queue, ^{
sleep(4);
NSLog(@"1");
});
dispatch_group_async(group, queue, ^{
sleep(3);
NSLog(@"2");
});
dispatch_group_async(group, queue, ^{
sleep(2);
NSLog(@"3");
});
dispatch_group_async(group, queue, ^{
sleep(1);
NSLog(@"4");
});
dispatch_group_notify(group, queue, ^{
NSLog(@"notify");
});
2020-11-09 16:19:38.606736+0800 Demo[47641:2507387] 4
2020-11-09 16:19:39.605344+0800 Demo[47641:2507390] 3
2020-11-09 16:19:40.606060+0800 Demo[47641:2507389] 2
2020-11-09 16:19:41.605860+0800 Demo[47641:2507391] 1
2020-11-09 16:19:41.606227+0800 Demo[47641:2507391] notify
3、处理耗时操作 dispatch_group_enter、dispatch_group_leave
1、dispatch_group_enter 向 group追加一个任务,group 中未执行完任务数+1。
2、dispatch_group_leave 在group删去一个任务,group 中未执行完任务数-1。
3、当 group 中未执行完任务数为0时,会使dispatch_group_wait解除阻塞,然后执行dispatch_group_notify block中的任务。
dispatch_group_t group = dispatch_group_create();
dispatch_queue_t queue = dispatch_queue_create("cwwng-queue", DISPATCH_QUEUE_CONCURRENT);
dispatch_group_enter(group);
dispatch_group_async(group, queue, ^{
// 模拟网络请求,耗时操作
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(4.0 * NSEC_PER_SEC)), queue, ^{
NSLog(@"1");
dispatch_group_leave(group);
});
});
dispatch_group_enter(group);
dispatch_group_async(group, queue, ^{
// 模拟网络请求,耗时操作
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3.0 * NSEC_PER_SEC)), queue, ^{
NSLog(@"2");
dispatch_group_leave(group);
});
});
dispatch_group_enter(group);
dispatch_group_async(group, queue, ^{
// 模拟网络请求,耗时操作
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), queue, ^{
NSLog(@"3");
dispatch_group_leave(group);
});
});
dispatch_group_enter(group);
dispatch_group_async(group, queue, ^{
// 模拟网络请求,耗时操作
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), queue, ^{
NSLog(@"4");
dispatch_group_leave(group);
});
});
dispatch_group_notify(group, queue, ^{
NSLog(@"notify");
});
2020-11-09 16:28:37.651037+0800 Demo[48060:2516999] 4
2020-11-09 16:28:38.650977+0800 Demo[48060:2517002] 3
2020-11-09 16:28:39.650961+0800 Demo[48060:2516999] 2
2020-11-09 16:28:40.651016+0800 Demo[48060:2517002] 1
2020-11-09 16:28:40.651360+0800 Demo[48060:2517002] notify
4、dispatch_group_wait
阻塞当前线程,等待指定的 group 中的任务执行完成后,才会往下继续执行。
dispatch_group_t group = dispatch_group_create();
dispatch_queue_t queue = dispatch_queue_create("cwwng-queue", DISPATCH_QUEUE_CONCURRENT);
dispatch_group_enter(group);
dispatch_group_async(group, queue, ^{
// 模拟网络请求,耗时操作
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(4.0 * NSEC_PER_SEC)), queue, ^{
NSLog(@"1");
dispatch_group_leave(group);
});
});
dispatch_group_enter(group);
dispatch_group_async(group, queue, ^{
// 模拟网络请求,耗时操作
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3.0 * NSEC_PER_SEC)), queue, ^{
NSLog(@"2");
dispatch_group_leave(group);
});
});
dispatch_group_enter(group);
dispatch_group_async(group, queue, ^{
// 模拟网络请求,耗时操作
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), queue, ^{
NSLog(@"3");
dispatch_group_leave(group);
});
});
dispatch_group_enter(group);
dispatch_group_async(group, queue, ^{
// 模拟网络请求,耗时操作
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), queue, ^{
NSLog(@"4");
dispatch_group_leave(group);
});
});
NSLog(@"wait 前");
dispatch_group_wait(group, DISPATCH_TIME_FOREVER);
NSLog(@"wait 后");
2020-11-09 16:33:04.240531+0800 Demo[48264:2521815] wait 前
2020-11-09 16:33:05.240633+0800 Demo[48264:2521916] 4
2020-11-09 16:33:06.240647+0800 Demo[48264:2521910] 3
2020-11-09 16:33:07.240654+0800 Demo[48264:2521910] 2
2020-11-09 16:33:08.243215+0800 Demo[48264:2521910] 1
2020-11-09 16:33:08.243479+0800 Demo[48264:2521815] wait 后
网友评论