标签:gcd dispatch_group dispatch_group_enter async 嵌套回调
今天被一个问题纠结了2个小时,一开始失败的代码大概是这样的:
dispatch_group_t group = dispatch_group_create(); while([rs next]){ dispatch_group_async(group, dispatch_get_global_queue(0, 0), ^{ [httpHelper getSecure:url completionHandler:^(NSDictionary* dict){ // 子任务 }]; }); } dispatch_group_notify(group, dispatch_get_global_queue(0, 0), ^{ // 子任务全部完成后,才执行 });
或者换一种写法,把dispatch_group_async写到getSecure的callback里面,也不行
最后终于找到一个救命帖:
gcd dispatch groups with an additional level of inception
应该写成这样:
dispatch_group_t group = dispatch_group_create(); while([rs next]){ dispatch_group_enter(group); dispatch_async(dispatch_get_global_queue(0, 0), ^{ [httpHelper getSecure:url completionHandler:^(NSDictionary* dict){ // 子任务 dispatch_group_leave(group); }]; }); } dispatch_group_notify(group, dispatch_get_global_queue(0, 0), ^{ // 子任务全部完成后,才执行 });
标签:gcd dispatch_group dispatch_group_enter async 嵌套回调
原文地址:http://blog.csdn.net/kyfxbl/article/details/31774627