ios 多个网络请求成功以后,在回调给H5

  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,有点像信号量的使用,它俩是成对出现的.

网上找的一张图,留待研究

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值