標籤:
先看一段錯誤的方法:
- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. NSURLSession *session = [NSURLSession sharedSession]; dispatch_queue_t dispatchQueue = dispatch_queue_create("test.queue", DISPATCH_QUEUE_CONCURRENT); dispatch_group_t dispatchGroup = dispatch_group_create(); dispatch_group_async(dispatchGroup, dispatchQueue, ^(){ NSURLSessionDataTask *task = [session dataTaskWithURL:[NSURL URLWithString:@"https://www.baidu.com"] completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) { NSLog(@"got data from internet1"); }]; [task resume]; }); dispatch_group_async(dispatchGroup, dispatchQueue, ^(){ NSURLSessionDataTask *task = [session dataTaskWithURL:[NSURL URLWithString:@"https://www.baidu.com"] completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) { NSLog(@"got data from internet2"); }]; [task resume]; }); dispatch_group_notify(dispatchGroup, dispatch_get_main_queue(), ^(){ NSLog(@"end"); });}
看看log的輸出
2016-07-13 17:42:55.170 aaaa[4797:295528] end2016-07-13 17:42:55.322 aaaa[4797:295574] got data from internet22016-07-13 17:42:55.375 aaaa[4797:295574] got data from internet1
完全沒有達到效果。這是因為這裡的網路請求是個非同步方法,沒有等待具體的資料返回,放入的dispatch queue的 block就執行完畢了。所以沒收到2個網路資料,就提前調用了dispatch_group_notify指定的結束方法。
再看正確的方法:
- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. NSURLSession *session = [NSURLSession sharedSession]; dispatch_queue_t dispatchQueue = dispatch_queue_create("test.queue", DISPATCH_QUEUE_CONCURRENT); dispatch_group_t dispatchGroup = dispatch_group_create(); // dispatch_group_async(dispatchGroup, dispatchQueue, ^(){ dispatch_group_enter(dispatchGroup); NSURLSessionDataTask *task = [session dataTaskWithURL:[NSURL URLWithString:@"https://www.baidu.com"] completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) { NSLog(@"got data from internet1"); dispatch_group_leave(dispatchGroup); }]; [task resume]; // }); // dispatch_group_async(dispatchGroup, dispatchQueue, ^(){ dispatch_group_enter(dispatchGroup); NSURLSessionDataTask *task2 = [session dataTaskWithURL:[NSURL URLWithString:@"https://www.baidu.com"] completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) { NSLog(@"got data from internet2"); dispatch_group_leave(dispatchGroup); }]; [task2 resume]; // }); dispatch_group_notify(dispatchGroup, dispatch_get_main_queue(), ^(){ NSLog(@"end"); });}
看正確是輸出結果:
2016-07-13 17:46:10.282 aaaa[4847:300370] got data from internet12016-07-13 17:46:10.501 aaaa[4847:300370] got data from internet22016-07-13 17:46:10.502 aaaa[4847:300341] end
我們看看關於dispatch_group_enter 的具體說明
Calling this function increments the current count of outstanding tasks in the group. Using this function (with dispatch_group_leave) allows your application to properly manage the task reference count if it explicitly adds and removes tasks from the group by a means other than using the dispatch_group_async function. A call to this function must be balanced with a call to dispatch_group_leave. You can use this function to associate a block with more than one group at the same time.
簡單的說,就是dispatch_group_enter會對group的內部計數加一,dispatch_group_leave會對group的內部計數減一,就類似以前的retain和release方法。說白了也是維護了一個計數器,只不過系統將這個計數器封裝了起來,不用我們自己實現了,代碼看起來更簡潔,高大上了。
iOS 多個非同步網路請求全部返回後再執行具體邏輯的方法