dispatch_group_t group = dispatch_group_create();
dispatch_group_enter(group);
dispatch_group_async(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0), ^{
//正面图片上传后台
NSMutableDictionary *dicc = [NSMutableDictionary dictionaryWithDictionary:self.idCardDic];
[self OCrCheck_saveImageKey:frontFilekey dic: dicc complete:^(int res) {
dispatch_group_leave(group);
}];
});
dispatch_group_enter(group);
dispatch_group_async(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
//背面图片上传后台
NSMutableDictionary *dicb = [NSMutableDictionary dictionaryWithDictionary:self.idCardDic];
[self OCrCheck_saveImageKey:backFilekey dic:dicb complete:^(int res) {
dispatch_group_leave(group);
}];
});
dispatch_group_notify(group, dispatch_get_main_queue(), ^{
NSLog(@"上传H5");
});
使用GCD的group
1、dispatch_group_create 创建一个调度任务组
2、dispatch_group_async 把一个任务异步提交到任务组里
3、dispatch_group_enter :通知 group,下个任务要放入 group 中执行了
4、dispatch_group_leave: 通知 group,任务成功完成,要移除,与 enter成对出现
5、dispatch_group_notify 用来监听任务组事件的执行完毕
6、dispatch_group_wait 设置等待时间
坑点就在于,dispatch_group_async中的任务是网络请求,是异步执行的,这就导致 dispatch_group_async不用等待网络请求的任务完成就结束了,dispatch_group_notify监听就认为任务已经完成了。
解决方法就是使用 dispatch_group_enter和dispatch_group_leave,有点像信号量的使用,它俩是成对出现的.
网上找的一张图,留待研究