iOS多个网络请求问题

开发中,有时候一个页面需要多个网络请求之后才能更新ui,这时候我们可以用嵌套请求方法,但是如果网络请求比较多,那么嵌套方法肯定不行了,太麻烦对吧,这时候不放用一下gcd的信号量

 // 创建组
        [MBProgressHUD showMessage:@"加载中..."];
        dispatch_group_t group = dispatch_group_create();
        dispatch_group_async(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

            // 创建信号量
            dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);

            NSString *strurl=[HSTools Joint:URL_GUBRI];
            NSMutableDictionary *parameters=[NSMutableDictionary dictionary];
            parameters[@"region_id"]=self.region_id;
            [JSCHttpTool GET:strurl parameters:parameters success:^(id responseObject) {

                NSLog(@"网络一请求成功");

                if ([responseObject[@"code"] integerValue] == 1){

                    [self.unitArray removeAllObjects];

                    for (NSDictionary *unitsdict in responseObject[@"units"]) {
                        UnitModel *model=[[UnitModel alloc] init];
                        model.address=unitsdict[@"address"];
                        model.contact_number=unitsdict[@"contact_number"];
                        [self.unitArray addObject:model];

                    }
                    // 如果请求成功,发送信号量
                    dispatch_semaphore_signal(semaphore);

                  }
                } failure:^(NSError *error) {
                    NSLog(@"网络一请求失败");
                    [MBProgressHUD hideHUD];
                    //dispatch_semaphore_signal(semaphore);

                }];
            // 在网络请求任务成功之前,信号量等待中
            dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);

        });

        dispatch_group_async(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

            // 创建信号量
            dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);

            NSString *strurl=[HSTools Joint:URL_GUBRI];
            NSMutableDictionary *parameters=[NSMutableDictionary dictionary];
            parameters[@"region_id"]=self.region_id;
            [JSCHttpTool GET:strurl parameters:parameters success:^(id responseObject) {

                NSLog(@"网络二请求成功");
                if ([responseObject[@"code"] integerValue] == 1){

                    [self.otherunitArray removeAllObjects];

                    for (NSDictionary *unitsdict in responseObject[@"units"]) {
                        UnitModel *model=[[UnitModel alloc] init];
                        model.address=unitsdict[@"address"];
                        model.contact_number=unitsdict[@"contact_number"];
                        [self.otherunitArray addObject:model];

                    }
                    // 如果请求成功,发送信号量
                    dispatch_semaphore_signal(semaphore);

                }
            } failure:^(NSError *error) {
                NSLog(@"网络二请求失败");
                [MBProgressHUD hideHUD];
               // dispatch_semaphore_signal(semaphore);

            }];
            // 在网络请求任务成功之前,信号量等待中
            dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);

        });


        dispatch_group_notify(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            [MBProgressHUD hideHUD];
            NSLog(@"完成了网络请求,并且都成功。");
            NSLog(@"self.unitArray.count--%lu",(unsigned long)self.unitArray.count);
             NSLog(@"self.otherunitArray.count--%lu",(unsigned long)self.otherunitArray.count);
        });

代码中,我在每个failure里面都写了一行注释掉的// dispatch_semaphore_signal(semaphore);
是因为,如果有时候我们需要不论请求成功还是失败,都走dispatch_group_notify方法的话,就可以打开此处的注释。在这里我是需要两个请求全部成功之后才允许走dispatch_group_notify方法的。

补充:经测试dispatch_group_notify里面更新ui的方法十分的卡,具体为什么,请大神评论告知

解决方法:已经找到解决方法,多个网络请求结束之后我们需要回到主线程刷新ui界面。

以下是更改过后的代码

// 创建组
        [MBProgressHUD showMessage:@"加载中..."];
        dispatch_group_t group = dispatch_group_create();
        dispatch_group_async(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

            // 创建信号量
            dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);

            NSString *strurl=[HSTools Joint:URL_GUBRI];
            NSMutableDictionary *parameters=[NSMutableDictionary dictionary];
            parameters[@"region_id"]=self.region_id;
            [JSCHttpTool GET:strurl parameters:parameters success:^(id responseObject) {

                NSLog(@"网络一请求成功");

                if ([responseObject[@"code"] integerValue] == 1){

                    [self.unitArray removeAllObjects];

                    for (NSDictionary *unitsdict in responseObject[@"units"]) {
                        UnitModel *model=[[UnitModel alloc] init];
                        model.address=unitsdict[@"address"];
                        model.contact_number=unitsdict[@"contact_number"];
                        [self.unitArray addObject:model];

                    }
                    // 如果请求成功,发送信号量
                    dispatch_semaphore_signal(semaphore);

                  }
                } failure:^(NSError *error) {
                    NSLog(@"网络一请求失败");
                    [MBProgressHUD hideHUD];
                    //dispatch_semaphore_signal(semaphore);

                }];
            // 在网络请求任务成功之前,信号量等待中
            dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);

        });

        dispatch_group_async(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

            // 创建信号量
            dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);

            NSString *strurl=[HSTools Joint:URL_GUBRI];
            NSMutableDictionary *parameters=[NSMutableDictionary dictionary];
            parameters[@"region_id"]=self.region_id;
            [JSCHttpTool GET:strurl parameters:parameters success:^(id responseObject) {

                NSLog(@"网络二请求成功");
                if ([responseObject[@"code"] integerValue] == 1){

                    [self.otherunitArray removeAllObjects];

                    for (NSDictionary *unitsdict in responseObject[@"units"]) {
                        UnitModel *model=[[UnitModel alloc] init];
                        model.address=unitsdict[@"address"];
                        model.contact_number=unitsdict[@"contact_number"];
                        [self.otherunitArray addObject:model];

                    }
                    // 如果请求成功,发送信号量
                    dispatch_semaphore_signal(semaphore);

                }
            } failure:^(NSError *error) {
                NSLog(@"网络二请求失败");
                [MBProgressHUD hideHUD];
               // dispatch_semaphore_signal(semaphore);

            }];
            // 在网络请求任务成功之前,信号量等待中
            dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);

        });


        dispatch_group_notify(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            dispatch_async(dispatch_get_main_queue(), ^{

             [MBProgressHUD hideHUD];
              NSLog(@"完成了网络请求,并且都成功。");
            NSLog(@"self.unitArray.count--%lu",(unsigned long)self.unitArray.count);
             NSLog(@"self.otherunitArray.count--%lu",(unsigned long)self.otherunitArray.count);

             });

//    也可以加判断语句
//    if (判断语句) {
//        // 返回主线程进行界面上的修改
//        dispatch_async(dispatch_get_main_queue(), ^{
//            更新ui
//        });
//    }else{
//        dispatch_async(dispatch_get_main_queue(), ^{
//           更新ui
//        });
//    }
        });
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值