许布?兴革??蜜唾?如果想在dispatch_queue中所有的任務(wù)執(zhí)行完成后在做某種操作杂曲,在串行隊(duì)列中,可以把該操作放到最后一個(gè)任務(wù)執(zhí)行完成后繼續(xù)袁余,但是在并行隊(duì)列中怎么做呢擎勘。這就有dispatch_group 成組操作。比如
dispatch_queue_t dispatchQueue = dispatch_queue_create("ted.queue.next", DISPATCH_QUEUE_CONCURRENT);
dispatch_group_t dispatchGroup = dispatch_group_create();
dispatch_group_async(dispatchGroup, dispatchQueue, ^(){
NSLog(@"dispatch-1");
});
dispatch_group_async(dispatchGroup, dispatchQueue, ^(){
NSLog(@"dspatch-2");
});
dispatch_group_notify(dispatchGroup, dispatch_get_main_queue(), ^(){
NSLog(@"end");
});
上面的 log1 和log2輸出順序不定颖榜,因?yàn)槭窃诓⑿嘘?duì)列上執(zhí)行棚饵,當(dāng)并行隊(duì)列全部執(zhí)行完成后,最后到main隊(duì)列上執(zhí)行一個(gè)操作掩完,保證“end”是最后輸出噪漾。 另外,這里也可以不用創(chuàng)建自己的并行隊(duì)列且蓬,用全局的global欣硼,那個(gè)也是個(gè)并行隊(duì)列. dispatch_get_gloable_queue(0,0);