?? Dispath Group機(jī)制, 根據(jù)系統(tǒng)資源狀況執(zhí)行任務(wù) - >條件需要等待多個(gè)并行任務(wù)結(jié)束時(shí) 執(zhí)行某個(gè)任務(wù), 這個(gè)時(shí)候使用dispath group 實(shí)現(xiàn);
?? 通過(guò)Dispath Group 函數(shù), 可以把并發(fā)執(zhí)行的多個(gè)任務(wù)合為一組, 調(diào)用者就可以知道這些任務(wù)何時(shí)才能全部執(zhí)行完畢;
//一個(gè)優(yōu)先級(jí)低的并發(fā)隊(duì)列
dispatch_queue_t lowPriorityQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0);
//一個(gè)優(yōu)先級(jí)高的并發(fā)隊(duì)列
dispatch_queue_t highPriorityQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0);
//創(chuàng)建dispatch_group
dispatch_group_t dispatchGroup = dispatch_group_create();
//將優(yōu)先級(jí)低的隊(duì)列放入dispatch_group
for(id objectinlowPriorityObjects) {
dispatch_group_async(dispatchGroup,lowPriorityQueue,^{ [object performTask]; });
}
//將優(yōu)先級(jí)高的隊(duì)列放入dispatch_group
for(id objectinhighPriorityObjects) {
dispatch_group_async(dispatchGroup,highPriorityQueue,^{ [object performTask]; });
}
//dispatch_group里的任務(wù)都結(jié)束后調(diào)用塊中的代碼
dispatch_queue_t notifyQueue = dispatch_get_main_queue();
dispatch_group_notify(dispatchGroup,notifyQueue,^{
? ? // Continue processing after completing tasks
});