Dispath Group机制, 根据系统资源状况执行任务 - >条件需要等待多个并行任务结束时 执行某个任务, 这个时候使用dispath group 实现;
通过Dispath Group 函数, 可以把并发执行的多个任务合为一组, 调用者就可以知道这些任务何时才能全部执行完毕;
//一个优先级低的并发队列
dispatch_queue_t lowPriorityQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0);
//一个优先级高的并发队列
dispatch_queue_t highPriorityQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0);
//创建dispatch_group
dispatch_group_t dispatchGroup = dispatch_group_create();
//将优先级低的队列放入dispatch_group
for(id objectinlowPriorityObjects) {
dispatch_group_async(dispatchGroup,lowPriorityQueue,^{ [object performTask]; });
}
//将优先级高的队列放入dispatch_group
for(id objectinhighPriorityObjects) {
dispatch_group_async(dispatchGroup,highPriorityQueue,^{ [object performTask]; });
}
//dispatch_group里的任务都结束后调用块中的代码
dispatch_queue_t notifyQueue = dispatch_get_main_queue();
dispatch_group_notify(dispatchGroup,notifyQueue,^{
// Continue processing after completing tasks
});
网友评论