版本:iOS13.5
group.h
索引
-
dispatch_group_create
创建可以与block关联的组 -
dispatch_group_async
将block提交到调度队列queue并异步执行,并将block与给定的调度组group关联。 -
dispatch_group_async_f
将函数work提交给调度队列queue并异步执行,并将work与给定的调度组group关联。 -
dispatch_group_wait
同步等待,直到与group关联的所有block都已完成,或者直到超时为止。(会堵塞当前线程) -
dispatch_group_notify
与group关联的所有块均已完成后,将block提交给队列queue执行。 -
dispatch_group_notify_f
与group关联的所有函数均已完成后,将work提交给队列queue执行。 -
dispatch_group_enter
手动将group的计数加1 -
dispatch_group_leave
手动将group的计数减1
group的计数为0时才会告诉dispatch_group_wait
和dispatch_group_notify
group中所有关联的block已完成
详解
- 创建可以与block关联的组。
dispatch_group_t dispatch_group_create(void);
- 将block提交到调度队列queue并异步执行,并将block与给定的调度组group关联。
void dispatch_group_async(dispatch_group_t group, dispatch_queue_t queue,
dispatch_block_t block);
例:
dispatch_queue_t queue = dispatch_queue_create("Dispatch_group", DISPATCH_QUEUE_CONCURRENT);
dispatch_group_t group = dispatch_group_create();
dispatch_group_async(group, queue, ^{
NSLog(@"block1");
sleep(1);
});
dispatch_group_async(group, queue, ^{
NSLog(@"block2");
sleep(1);
});
dispatch_group_async(group, queue, ^{
NSLog(@"block3");
sleep(1);
});
输出:因为queue是并行的 所以结果为132 也会出现123
block1
block3
block2
- 将函数work提交给调度队列queue并异步执行,并将work与给定的调度组group关联。
void dispatch_group_async_f(dispatch_group_t group, dispatch_queue_t queue,
void *_Nullable context, dispatch_function_t work);
work 函数typedef void (*dispatch_function_t)(void *_Nullable)
context work传入的参数(传该参数的地址)
dispatch_group_async_f
相较于dispatch_group_async
就是将block变成work函数
例:
dispatch_queue_t queue = dispatch_queue_create("Dispatch_group", DISPATCH_QUEUE_CONCURRENT);
dispatch_group_t group = dispatch_group_create();
dispatch_group_async_f(group, queue, nil, test);
void test(void *context) {
NSLog(@"test");
}
输出:
test
- 同步等待,直到与group关联的所有block都已完成,或者直到超时为止。(会堵塞当前线程)
long dispatch_group_wait(dispatch_group_t group, dispatch_time_t timeout);
timeout DISPATCH_TIME_FOREVER
表示永不超时 DISPATCH_TIME_NOW
表示立即超时 或dispatch_time(DISPATCH_TIME_NOW, 1 * NSEC_PER_SEC)
指定时间
long 与该组block关联的所有block在超时时间内完成则返回0,超时则返回非0
例:
dispatch_queue_t queue = dispatch_queue_create("Dispatch_group", DISPATCH_QUEUE_CONCURRENT);
dispatch_group_t group = dispatch_group_create();
dispatch_group_async(group, queue, ^{
sleep(1);
NSLog(@"block1");
});
dispatch_group_async(group, queue, ^{
sleep(1);
NSLog(@"block2");
});
dispatch_group_async(group, queue, ^{
sleep(1);
NSLog(@"block3");
});
dispatch_async(queue, ^{
long count = dispatch_group_wait(group, dispatch_time(DISPATCH_TIME_NOW, 2 * NSEC_PER_SEC));
NSLog(@"count = %ld", count);
});
输出:因为sleep(1),block只执行了1秒,超时时间为2秒,所以count = 0表示未超时
block1
block3
block2
count = 0
若block2 sleep(3),则输出
block1
block3
count = 49
block2
超时时间2秒内block2未执行完,所有会超时 count = 49
- 与group关联的所有块均已完成后,将block提交给队列queue执行。
void dispatch_group_notify(dispatch_group_t group, dispatch_queue_t queue,
dispatch_block_t block);
当group中没有关联的块时 会直接执行block
例:
dispatch_queue_t queue = dispatch_queue_create("Dispatch_group", DISPATCH_QUEUE_CONCURRENT);
dispatch_group_t group = dispatch_group_create();
dispatch_group_async(group, queue, ^{
sleep(1);
NSLog(@"block1");
});
dispatch_group_async(group, queue, ^{
sleep(3);
NSLog(@"block2");
});
dispatch_group_async(group, queue, ^{
sleep(1);
NSLog(@"block3");
});
dispatch_group_notify(group, queue, ^{
NSLog(@"所有block都完成");
});
输出:
block1
block3
block2
所有block都完成
- 与group关联的所有函数均已完成后,将work提交给队列queue执行。
void dispatch_group_notify_f(dispatch_group_t group, dispatch_queue_t queue,
void *_Nullable context, dispatch_function_t work);
相关释义可查看dispatch_group_async_f
dispatch_group_notify_f
相较于dispatch_group_notify
就是将block变成了函数work
- 手动将group的计数加1
void dispatch_group_enter(dispatch_group_t group)
- 手动将group的计数减1
void dispatch_group_leave(dispatch_group_t group);
注意:dispatch_group_enter
和dispatch_group_leave
必须成对出现
dispatch_group_async
就是先让group的计数加1,待block执行完成后group的计数减1
dispatch_group_async(group, queue, ^{
});
相当于
dispatch_group_enter(group);
dispatch_async(queue, ^{
dispatch_group_leave(group);
});
group的计数为0时才会告诉dispatch_group_wait
和dispatch_group_notify
组group中所有关联的block已完成
例:
dispatch_queue_t queue = dispatch_queue_create("Dispatch_group", DISPATCH_QUEUE_CONCURRENT);
dispatch_group_t group = dispatch_group_create();
dispatch_group_async(group, queue, ^{
sleep(1);
NSLog(@"block1");
});
dispatch_group_enter(group);
dispatch_async(queue, ^{
sleep(3);
NSLog(@"block4");
dispatch_group_leave(group);
});
dispatch_group_notify(group, queue, ^{
NSLog(@"所有block都完成");
});
输出:
block1
block4
所有block都完成
网友评论