group.h
相关方法
方法介绍
/**
创建组
*/
dispatch_group_t
dispatch_group_create(void);
/**
组内部异步线程
@param group 组
@param queue 队列
@param block 任务
*/
void
dispatch_group_async(dispatch_group_t group,
dispatch_queue_t queue,
dispatch_block_t block);
/*
组内部异步线程
@param group 组
@param queue 队列
@param context 数据指针
@param work C函数方法名
*/
void
dispatch_group_async_f(dispatch_group_t group,
dispatch_queue_t queue,
void *_Nullable context,
dispatch_function_t work);
/*
等待组任务完成
@param group 指定被等待的组
@param timeout 等待的时长
*/
long
dispatch_group_wait(dispatch_group_t group, dispatch_time_t timeout);
/*
组队列中任务完成通知
@param group 监听的组
@param queue 监听的队列
@param block 任务完成回调
*/
void
dispatch_group_notify(dispatch_group_t group,
dispatch_queue_t queue,
dispatch_block_t block);
/*
组队列中任务完成通知
@param group 监听的组
@param queue 监听的队列
@param context 任务数据指针
@param work C函数方法名
*/
void
dispatch_group_notify_f(dispatch_group_t group,
dispatch_queue_t queue,
void *_Nullable context,
dispatch_function_t work);
/*
加入任务组
@param group 任务组
*/
void
dispatch_group_enter(dispatch_group_t group);
/*
离开任务组
@param group 任务组
*/
void
dispatch_group_leave(dispatch_group_t group);
举例说明:
dispatch_queue_t quene = dispatch_queue_create("com.Maker", DISPATCH_QUEUE_CONCURRENT);
dispatch_async(quene, ^{
dispatch_queue_t allTasksQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_group_t allTasksGroup = dispatch_group_create();
dispatch_group_async(allTasksGroup, allTasksQueue, ^{
NSLog(@"任务_1-----%d",[NSThread isMainThread]);
});
dispatch_group_async(allTasksGroup, allTasksQueue, ^{
NSLog(@"任务_2-----%d",[NSThread isMainThread]);
});
dispatch_group_async(allTasksGroup, allTasksQueue, ^{
NSLog(@"任务_3-----%d",[NSThread isMainThread]);
});
NSLog(@"--------------------");
dispatch_time_t time = dispatch_time(DISPATCH_TIME_FOREVER, 10 * NSEC_PER_SEC);
if (dispatch_group_wait(allTasksGroup, time) == 0) {
NSLog(@"任务已经全部执行完毕-----%d",[NSThread isMainThread]);
}
else {
NSLog(@"任务没有执行完毕-----%d",[NSThread isMainThread]);
}
/*
dispatch_group_notify(allTasksGroup, allTasksQueue, ^{
NSLog(@"任务已经全部执行完毕-----%d",[NSThread isMainThread]);
});
*/
});