在實(shí)際開發(fā)中吉执,我們需要按順序執(zhí)行一些異步的任務(wù),我們應(yīng)該怎么做呢?
答案是:dispatch_group_notify
這里我們使用手動(dòng)管理block的運(yùn)行狀態(tài)
dispatch_group_enter相當(dāng)于提交了一個(gè)任務(wù)
dispatch_group_leave相當(dāng)于完成了一個(gè)任務(wù)
這里需要強(qiáng)調(diào)一點(diǎn)
進(jìn)入和退出group次數(shù)必須匹配 如果少了leave話notify是永遠(yuǎn)都完成不了的
實(shí)例代碼如下:
dispatch_group_t serviceGroup = dispatch_group_create();
// Start the first task
dispatch_group_enter(serviceGroup);
[self startTask:5 Block:^(BOOL success) {
dispatch_group_leave(serviceGroup);
}];
// Start the second task
dispatch_group_enter(serviceGroup);
[self startTask:5 Block:^(BOOL success) {
dispatch_group_leave(serviceGroup);
}];
dispatch_group_notify(serviceGroup,dispatch_get_main_queue(),^{
NSLog(@"finish");
});
NSLog(@"next");
-
(void)startTask:(NSInteger)time Block:(someBlock)block
{
for (NSInteger i=0; i<time; i++) {NSLog(@"%ld",i);
}
block(YES);
}
輸出結(jié)果:
2015-05-16 14:46:31.401 syncTest[26054:9467066] 0
2015-05-16 14:46:31.401 syncTest[26054:9467066] 1
2015-05-16 14:46:31.402 syncTest[26054:9467066] 2
2015-05-16 14:46:31.402 syncTest[26054:9467066] 3
2015-05-16 14:46:31.402 syncTest[26054:9467066] 4
2015-05-16 14:46:31.402 syncTest[26054:9467066] 0
2015-05-16 14:46:31.402 syncTest[26054:9467066] 1
2015-05-16 14:46:31.402 syncTest[26054:9467066] 2
2015-05-16 14:46:31.402 syncTest[26054:9467066] 3
2015-05-16 14:46:31.402 syncTest[26054:9467066] 4
2015-05-16 14:46:31.403 syncTest[26054:9467066] next
2015-05-16 14:46:31.421 syncTest[26054:9467066] finish
這里看出任務(wù)是頭尾相接得熙掺,只有dispatch_group_leave之后下一個(gè)任務(wù)才會(huì)進(jìn)行咕宿。
這里需要注意的是我們?cè)赿ispatchgetmain_queue()主線程的隊(duì)列里面執(zhí)行的币绩,不適合運(yùn)行長(zhǎng)時(shí)間的任務(wù)府阀,一般使用dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
如果不在乎分組內(nèi)的任務(wù)的順序,可以使用dispatch_group_async
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_group_t serviceGroup = dispatch_group_create();
// Start the first task
dispatch_group_async(serviceGroup, queue, ^{
[self startTask:5 Block:^(BOOL success) {
}];
});
// Start the second task
dispatch_group_async(serviceGroup, queue, ^{
[self startTask:10 Block:^(BOOL success) {
}];
});
dispatch_group_notify(serviceGroup,queue,^{
NSLog(@"finish");
});
NSLog(@"next");
輸出結(jié)果:
2015-05-16 14:35:26.978 syncTest[25914:9451334] next
2015-05-16 14:35:26.978 syncTest[25914:9451392] 0
2015-05-16 14:35:26.978 syncTest[25914:9451393] 0
2015-05-16 14:35:26.979 syncTest[25914:9451393] 1
2015-05-16 14:35:26.979 syncTest[25914:9451393] 2
2015-05-16 14:35:26.979 syncTest[25914:9451393] 3
2015-05-16 14:35:26.979 syncTest[25914:9451393] 4
2015-05-16 14:35:26.979 syncTest[25914:9451393] 5
2015-05-16 14:35:26.979 syncTest[25914:9451393] 6
2015-05-16 14:35:26.979 syncTest[25914:9451393] 7
2015-05-16 14:35:26.979 syncTest[25914:9451393] 8
2015-05-16 14:35:26.980 syncTest[25914:9451393] 9
2015-05-16 14:35:26.979 syncTest[25914:9451392] 1
2015-05-16 14:35:26.980 syncTest[25914:9451392] 2
2015-05-16 14:35:26.980 syncTest[25914:9451392] 3
2015-05-16 14:35:26.980 syncTest[25914:9451392] 4
2015-05-16 14:35:26.980 syncTest[25914:9451392] finish
這里任務(wù)1和任務(wù)2的調(diào)用雖然是按照隊(duì)列來的董瞻,但是因?yàn)槭莃lock,所以執(zhí)行的同時(shí)的钠糊,而全部完成之后才會(huì)調(diào)用完成方法挟秤,對(duì)于整個(gè)group來說是異步的抄伍,一旦運(yùn)行之后艘刚,next的被調(diào)用時(shí)間是不確定的截珍。