实现多请求数据同步需要用到线程通知
1.首先传建一个线程组
int __block i = 0;
NSMutableArray __block*arr = @[].mutableCopy;
dispatch_group_tgroup =dispatch_group_create();
2.进入线程组创建几条异步线程网络请求,并在请求成功突出线程组
dispatch_group_enter(group);
dispatch_group_async(group,dispatch_get_global_queue(0,0), ^{
//线程一
[BaseViewModelbaseRequestWithURLStr:Url1WithDic:nilBackData:^(NSDictionary*model,NSString*error) {
NSLog(@"model1");
[arr addObject:model];
i++;
dispatch_group_leave(group);
}];
});
dispatch_group_enter(group);
dispatch_group_async(group,dispatch_get_global_queue(0,0), ^{
//线程二
[BaseViewModelbaseRequestWithURLStr:Url2WithDic:nilBackData:^(NSDictionary*model,NSString*error) {
NSLog(@"model2");
[arr addObject:model];
i++;
dispatch_group_leave(group);
}];
});
3.当线程组任务都结束的时候回调用线程通知block
//汇总mode
dispatch_group_notify(group,dispatch_get_global_queue(0,0), ^{
NSLog(@"--%@<>%d--",arr,i);
}
});