
Snip20211217_7.png
dispatch_group_t group = dispatch_group_create();
for ( int i = 0; i < 10; i++) {
// 把一个异步操作加入到group中
dispatch_group_enter(group);
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSURLSessionDataTask * dataTask = [[NSURLSession sharedSession] dataTaskWithURL:[NSURL URLWithString:@"https://www.baidu.com"] completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
NSString * str = [NSString stringWithFormat:@"%d asdfa",i];
NSLog(@"--==-str : %@---",str);
dispatch_group_leave(group);
}];
[dataTask resume];
});
}
// 等待group完成所有异步操作
dispatch_group_notify(group, dispatch_get_main_queue(), ^{
NSLog(@"--==- finish ---");
});
网友评论