开发中,有时候一个页面需要多个网络请求之后才能更新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// });// } });新闻热点
疑难解答