// 创建组 [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// });// } });
网友评论