使用GCD组加载多个网络请求 全部返回数据好调用 nofify进行数据的整理:
方法1:
NSArray * urls = @[@"https://blog.csdn.net/li198847/article/details/85222664",@"//www.greatytc.com/p/449fd6c8993b",@"https://www.cnblogs.com/wfwenchao/articles/3688231.html"];
dispatch_group_t group = dispatch_group_create();
dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc] initWithSessionConfiguration:configuration];
AFSecurityPolicy * AFSec =[AFSecurityPolicy defaultPolicy];
manager.securityPolicy = AFSec;
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
for (int i = 0; i<100; i++) {
dispatch_group_enter(group);
NSLog(@"当前线程:%@",[NSThread currentThread]);
[manager GET:urls[0] parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
[self.htmlStrArr addObject:responseObject];
dispatch_group_leave(group);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
[self.htmlStrArr addObject:error];
dispatch_group_leave(group);
}];
}
dispatch_group_notify(group, queue, ^{
NSLog(@"htmlStrArr ==---- %lu",(unsigned long)self.htmlStrArr.count);
});
方法2:
NSArray * urls = @[@"https://blog.csdn.net/li198847/article/details/85222664",@"//www.greatytc.com/p/449fd6c8993b",@"https://www.cnblogs.com/wfwenchao/articles/3688231.html"];
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc] initWithSessionConfiguration:configuration];
AFSecurityPolicy * AFSec =[AFSecurityPolicy defaultPolicy];
manager.securityPolicy = AFSec;
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
dispatch_async(dispatch_get_global_queue(0, 0), ^{
dispatch_group_t group = dispatch_group_create();
for (int i = 0; i<100; i++) {
dispatch_group_enter(group);
NSLog(@"当前线程:%@",[NSThread currentThread]);
[manager GET:urls[0] parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
[self.htmlStrArr addObject:responseObject];
dispatch_group_leave(group);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
[self.htmlStrArr addObject:error];
dispatch_group_leave(group);
}];
}
dispatch_group_wait(group, DISPATCH_TIME_FOREVER);
dispatch_async(dispatch_get_global_queue(0, 0), ^{
NSLog(@"一共请求个数:%ld",self.htmlStrArr.count);
});
});