GCD:異步+并發(fā)(信號量)
requestNetworking要保證請求完成才可以再次請求加個判斷:isFinishLoading
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
[self requestNetworking];
}
-(void)requestNetworking{
dispatch_group_t dispatchGroup = dispatch_group_create();
dispatch_queue_t dispatchQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
//第一個網(wǎng)絡(luò)請求
dispatch_group_async(dispatchGroup, dispatchQueue, ^{
NSLog(@"--------group1--------%@",[NSThread currentThread]);
[self test1];
});
//第二個網(wǎng)絡(luò)請求
dispatch_group_async(dispatchGroup, dispatchQueue, ^{
NSLog(@"--------group2--------%@",[NSThread currentThread]);
[self test2];
});
//第三個網(wǎng)絡(luò)請求
dispatch_group_async(dispatchGroup, dispatchQueue, ^{
NSLog(@"--------group3--------%@",[NSThread currentThread]);
[self test3];
});
//第四個網(wǎng)絡(luò)請求
dispatch_group_async(dispatchGroup, dispatchQueue, ^{
NSLog(@"--------group4--------%@",[NSThread currentThread]);
[self test4];
});
//第五個網(wǎng)絡(luò)請求
dispatch_group_async(dispatchGroup, dispatchQueue, ^{
NSLog(@"--------group5--------%@",[NSThread currentThread]);
[self test5];
});
dispatch_group_notify(dispatchGroup, dispatch_get_main_queue(), ^{
NSLog(@"--------updateUi--------%@",[NSThread currentThread]);
});
}
-(void)test1{
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:@"https://www.baidu.com"]];
NSURLSessionDownloadTask *task = [[NSURLSession sharedSession] downloadTaskWithRequest:request completionHandler:^(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error) {
NSLog(@"第一步網(wǎng)絡(luò)請求完成");
dispatch_semaphore_signal(semaphore);
}];
[task resume];
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
}
-(void)test2{
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:@"https://www.jisilu.cn"]];
NSURLSessionDownloadTask *task = [[NSURLSession sharedSession] downloadTaskWithRequest:request completionHandler:^(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error) {
NSLog(@"第二步網(wǎng)絡(luò)請求完成");
dispatch_semaphore_signal(semaphore);
}];
[task resume];
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
}
-(void)test3{
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:@"https://www.hao123.com"]];
NSURLSessionDownloadTask *task = [[NSURLSession sharedSession] downloadTaskWithRequest:request completionHandler:^(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error) {
NSLog(@"第三步網(wǎng)絡(luò)請求完成");
dispatch_semaphore_signal(semaphore);
}];
[task resume];
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
}
-(void)test4{
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:@"https://developers.adnet.qq.com/doc/ios/guide"]];
NSURLSessionDownloadTask *task = [[NSURLSession sharedSession] downloadTaskWithRequest:request completionHandler:^(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error) {
NSLog(@"第四步網(wǎng)絡(luò)請求完成");
dispatch_semaphore_signal(semaphore);
}];
[task resume];
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
}
-(void)test5{
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:@"https://bugly.qq.com/v2"]];
NSURLSessionDownloadTask *task = [[NSURLSession sharedSession] downloadTaskWithRequest:request completionHandler:^(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error) {
NSLog(@"第五步網(wǎng)絡(luò)請求完成");
dispatch_semaphore_signal(semaphore);
}];
[task resume];
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
}
2023-04-28 11:30:52.339737+0800 text_04[3758:109469] --------group1--------<NSThread: 0x60000024c740>{number = 13, name = (null)}
2023-04-28 11:30:52.339833+0800 text_04[3758:109451] --------group2--------<NSThread: 0x600000247500>{number = 9, name = (null)}
2023-04-28 11:30:52.339854+0800 text_04[3758:109449] --------group3--------<NSThread: 0x60000021c6c0>{number = 7, name = (null)}
2023-04-28 11:30:52.339864+0800 text_04[3758:109470] --------group4--------<NSThread: 0x600000250540>{number = 15, name = (null)}
2023-04-28 11:30:52.339890+0800 text_04[3758:109453] --------group5--------<NSThread: 0x60000025df80>{number = 3, name = (null)}
2023-04-28 11:30:52.364842+0800 text_04[3758:109454] 第一步網(wǎng)絡(luò)請求完成
2023-04-28 11:30:52.399705+0800 text_04[3758:109456] 第五步網(wǎng)絡(luò)請求完成
2023-04-28 11:30:52.428851+0800 text_04[3758:109456] 第四步網(wǎng)絡(luò)請求完成
2023-04-28 11:30:52.430836+0800 text_04[3758:109470] 第二步網(wǎng)絡(luò)請求完成
2023-04-28 11:30:53.123856+0800 text_04[3758:109451] 第三步網(wǎng)絡(luò)請求完成
2023-04-28 11:30:53.124068+0800 text_04[3758:109415] --------updateUi--------<_NSMainThread: 0x600000218440>{number = 1, name = main}
在這個示例中,我們使用dispatch_group_async()函數(shù)將每個請求添加到dispatch_group_t中。對于每個請求,我們在一個dispatch_queue_t中執(zhí)行它,并使用dispatch_semaphore_t在請求完成時釋放信號量。我們在每個請求的末尾使用dispatch_semaphore_wait()函數(shù)等待信號量,以確保在處理完每個請求之前不會離開dispatch_group_t。最后,我們調(diào)用dispatch_group_notify()函數(shù)來指定所有請求完成后要執(zhí)行的代碼塊。
在這個示例中,dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER):放在子線程,不會阻塞主線程,可以執(zhí)行其他操作。
GCD:異步+并發(fā)(dispatch_group_enter)推薦用這個目前項目在使用
requestNetworking要保證請求完成才可以再次請求加個判斷:isFinishLoading
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
[self requestNetworking];
}
-(void)requestNetworking{
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
self.group = dispatch_group_create();
//第一個網(wǎng)絡(luò)請求
dispatch_group_enter(self.group);
dispatch_group_async(self.group, queue, ^{
NSLog(@"--------group1--------%@",[NSThread currentThread]);
[self test1];
});
//第二個網(wǎng)絡(luò)請求
dispatch_group_enter(self.group);
dispatch_group_async(self.group, queue, ^{
NSLog(@"--------group2--------%@",[NSThread currentThread]);
[self test2];
});
//第三個網(wǎng)絡(luò)請求
dispatch_group_enter(self.group);
dispatch_group_async(self.group, queue, ^{
NSLog(@"--------group3--------%@",[NSThread currentThread]);
[self test3];
});
//第四個網(wǎng)絡(luò)請求
dispatch_group_enter(self.group);
dispatch_group_async(self.group, queue, ^{
NSLog(@"--------group4--------%@",[NSThread currentThread]);
[self test4];
});
//第五個網(wǎng)絡(luò)請求
dispatch_group_enter(self.group);
dispatch_group_async(self.group, queue, ^{
NSLog(@"--------group5--------%@",[NSThread currentThread]);
[self test5];
});
dispatch_group_notify(self.group, dispatch_get_main_queue(), ^{
NSLog(@"--------updateUi--------%@",[NSThread currentThread]);
});
}
-(void)test1{
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:@"https://www.baidu.com"]];
NSURLSessionDownloadTask *task = [[NSURLSession sharedSession] downloadTaskWithRequest:request completionHandler:^(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error) {
NSLog(@"第一步網(wǎng)絡(luò)請求完成");
dispatch_group_leave(self.group);
}];
[task resume];
}
-(void)test2{
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:@"https://www.jisilu.cn"]];
NSURLSessionDownloadTask *task = [[NSURLSession sharedSession] downloadTaskWithRequest:request completionHandler:^(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error) {
NSLog(@"第二步網(wǎng)絡(luò)請求完成");
dispatch_group_leave(self.group);
}];
[task resume];
}
-(void)test3{
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:@"https://www.hao123.com"]];
NSURLSessionDownloadTask *task = [[NSURLSession sharedSession] downloadTaskWithRequest:request completionHandler:^(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error) {
NSLog(@"第三步網(wǎng)絡(luò)請求完成");
dispatch_group_leave(self.group);
}];
[task resume];
}
-(void)test4{
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:@"https://developers.adnet.qq.com/doc/ios/guide"]];
NSURLSessionDownloadTask *task = [[NSURLSession sharedSession] downloadTaskWithRequest:request completionHandler:^(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error) {
NSLog(@"第四步網(wǎng)絡(luò)請求完成");
dispatch_group_leave(self.group);
}];
[task resume];
}
-(void)test5{
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:@"https://bugly.qq.com/v2"]];
NSURLSessionDownloadTask *task = [[NSURLSession sharedSession] downloadTaskWithRequest:request completionHandler:^(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error) {
NSLog(@"第五步網(wǎng)絡(luò)請求完成");
dispatch_group_leave(self.group);
}];
[task resume];
}
2023-04-28 11:28:50.619371+0800 text_04[3663:105006] --------updateUi--------<_NSMainThread: 0x60000227c440>{number = 1, name = main}
2023-04-28 11:28:50.619658+0800 text_04[3663:105186] --------group1--------<NSThread: 0x600002271e00>{number = 7, name = (null)}
2023-04-28 11:28:50.619718+0800 text_04[3663:105187] --------group2--------<NSThread: 0x600002270f40>{number = 4, name = (null)}
2023-04-28 11:28:50.619749+0800 text_04[3663:105191] --------group3--------<NSThread: 0x600002220300>{number = 5, name = (null)}
2023-04-28 11:28:50.619763+0800 text_04[3663:105188] --------group4--------<NSThread: 0x60000221ea00>{number = 9, name = (null)}
2023-04-28 11:28:50.619794+0800 text_04[3663:105190] --------group5--------<NSThread: 0x600002230c80>{number = 3, name = (null)}
2023-04-28 11:28:50.652971+0800 text_04[3663:105187] 第一步網(wǎng)絡(luò)請求完成
2023-04-28 11:28:50.704591+0800 text_04[3663:105190] 第四步網(wǎng)絡(luò)請求完成
2023-04-28 11:28:50.709022+0800 text_04[3663:105190] 第二步網(wǎng)絡(luò)請求完成
2023-04-28 11:28:50.752817+0800 text_04[3663:105186] 第五步網(wǎng)絡(luò)請求完成
2023-04-28 11:28:51.462473+0800 text_04[3663:105187] 第三步網(wǎng)絡(luò)請求完成
2023-04-28 11:28:51.462692+0800 text_04[3663:105006] --------updateUi--------<_NSMainThread: 0x60000227c440>{number = 1, name = main}
在上面的代碼中,我們首先創(chuàng)建了一個dispatch_group_t類型的組,并將它添加到全局隊列中。然后,我們使用dispatch_group_enter函數(shù)進(jìn)入組,并分別發(fā)起了三個異步網(wǎng)絡(luò)請求,并在每個請求完成后打印出完成信息,并調(diào)用dispatch_group_leave函數(shù)以便離開組。最后,我們使用dispatch_group_notify函數(shù)在所有請求完成后打印出“所有異步網(wǎng)絡(luò)請求已完成”的信息。
GCD:異步+串行(信號量,開子線程按順序執(zhí)行)
requestNetworking要保證請求完成才可以再次請求加個判斷:isFinishLoading
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
[self requestNetworking];
}
-(void)requestNetworking{
dispatch_group_t dispatchGroup = dispatch_group_create();
dispatch_queue_t dispatchQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
self.semaphore = dispatch_semaphore_create(0);
//第一個網(wǎng)絡(luò)請求
dispatch_group_async(dispatchGroup, dispatchQueue, ^{
NSLog(@"--------group1--------%@",[NSThread currentThread]);
[self test1];
});
dispatch_semaphore_wait(self.semaphore, DISPATCH_TIME_FOREVER);
//第二個網(wǎng)絡(luò)請求
dispatch_group_async(dispatchGroup, dispatchQueue, ^{
NSLog(@"--------group2--------%@",[NSThread currentThread]);
[self test2];
});
dispatch_semaphore_wait(self.semaphore, DISPATCH_TIME_FOREVER);
//第三個網(wǎng)絡(luò)請求
dispatch_group_async(dispatchGroup, dispatchQueue, ^{
NSLog(@"--------group3--------%@",[NSThread currentThread]);
[self test3];
});
dispatch_semaphore_wait(self.semaphore, DISPATCH_TIME_FOREVER);
//第四個網(wǎng)絡(luò)請求
dispatch_group_async(dispatchGroup, dispatchQueue, ^{
NSLog(@"--------group4--------%@",[NSThread currentThread]);
[self test4];
});
dispatch_semaphore_wait(self.semaphore, DISPATCH_TIME_FOREVER);
//第五個網(wǎng)絡(luò)請求
dispatch_group_async(dispatchGroup, dispatchQueue, ^{
NSLog(@"--------group5--------%@",[NSThread currentThread]);
[self test5];
});
dispatch_semaphore_wait(self.semaphore, DISPATCH_TIME_FOREVER);
dispatch_group_notify(dispatchGroup, dispatch_get_main_queue(), ^{
NSLog(@"--------updateUi--------%@",[NSThread currentThread]);
});
}
-(void)test1{
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:@"https://www.baidu.com"]];
NSURLSessionDownloadTask *task = [[NSURLSession sharedSession] downloadTaskWithRequest:request completionHandler:^(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error) {
NSLog(@"第一步網(wǎng)絡(luò)請求完成");
dispatch_semaphore_signal(self.semaphore);
}];
[task resume];
}
-(void)test2{
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:@"https://www.jisilu.cn"]];
NSURLSessionDownloadTask *task = [[NSURLSession sharedSession] downloadTaskWithRequest:request completionHandler:^(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error) {
NSLog(@"第二步網(wǎng)絡(luò)請求完成");
dispatch_semaphore_signal(self.semaphore);
}];
[task resume];
}
-(void)test3{
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:@"https://www.hao123.com"]];
NSURLSessionDownloadTask *task = [[NSURLSession sharedSession] downloadTaskWithRequest:request completionHandler:^(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error) {
NSLog(@"第三步網(wǎng)絡(luò)請求完成");
dispatch_semaphore_signal(self.semaphore);
}];
[task resume];
}
-(void)test4{
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:@"https://developers.adnet.qq.com/doc/ios/guide"]];
NSURLSessionDownloadTask *task = [[NSURLSession sharedSession] downloadTaskWithRequest:request completionHandler:^(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error) {
NSLog(@"第四步網(wǎng)絡(luò)請求完成");
dispatch_semaphore_signal(self.semaphore);
}];
[task resume];
}
-(void)test5{
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:@"https://bugly.qq.com/v2"]];
NSURLSessionDownloadTask *task = [[NSURLSession sharedSession] downloadTaskWithRequest:request completionHandler:^(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error) {
NSLog(@"第五步網(wǎng)絡(luò)請求完成");
dispatch_semaphore_signal(self.semaphore);
}];
[task resume];
}
2023-04-28 15:20:02.338944+0800 text_04[2325:54425] --------group1--------<NSThread: 0x600000dc4280>{number = 7, name = (null)}
2023-04-28 15:20:02.356631+0800 text_04[2325:54425] 第一步網(wǎng)絡(luò)請求完成
2023-04-28 15:20:02.356897+0800 text_04[2325:54422] --------group2--------<NSThread: 0x600000dc9140>{number = 9, name = (null)}
2023-04-28 15:20:02.439213+0800 text_04[2325:54425] 第二步網(wǎng)絡(luò)請求完成
2023-04-28 15:20:02.439444+0800 text_04[2325:54421] --------group3--------<NSThread: 0x600000dd5d00>{number = 3, name = (null)}
2023-04-28 15:20:03.513870+0800 text_04[2325:54421] 第三步網(wǎng)絡(luò)請求完成
2023-04-28 15:20:03.514117+0800 text_04[2325:54421] --------group4--------<NSThread: 0x600000dd5d00>{number = 3, name = (null)}
2023-04-28 15:20:03.552748+0800 text_04[2325:54421] 第四步網(wǎng)絡(luò)請求完成
2023-04-28 15:20:03.552924+0800 text_04[2325:54425] --------group5--------<NSThread: 0x600000dc4280>{number = 7, name = (null)}
2023-04-28 15:20:03.685218+0800 text_04[2325:54421] 第五步網(wǎng)絡(luò)請求完成
2023-04-28 15:20:03.685468+0800 text_04[2325:54236] --------updateUi--------<_NSMainThread: 0x600000d94440>{number = 1, name = main}
在上面的示例中,每個網(wǎng)絡(luò)請求都是異步執(zhí)行的,并按順序執(zhí)行。在每個網(wǎng)絡(luò)請求執(zhí)行完成后,都通過dispatch_semaphore_signal方法使dispatch_semaphore_t對象的計數(shù)器加1。在每個網(wǎng)絡(luò)請求執(zhí)行之前,都需要通過dispatch_semaphore_wait方法等待dispatch_semaphore_t對象的計數(shù)器達(dá)到相應(yīng)的值。一旦所有請求完成,將會處理所有請求的結(jié)果。
在上面的示例中,dispatch_semaphore_wait(self.semaphore, DISPATCH_TIME_FOREVER):放在主線程,這個會阻塞主線程,其他操作執(zhí)行不了。只有當(dāng)執(zhí)行完網(wǎng)絡(luò)請求才能做其他事情
信號量
dispatch_semaphore_signal是GCD信號量函數(shù)之一,它的作用是增加信號量的計數(shù)器,從而解除由dispatch_semaphore_wait函數(shù)所阻塞的線程。
dispatch_semaphore_signal的參數(shù)是一個dispatch_semaphore_t對象,它是由dispatch_semaphore_create函數(shù)創(chuàng)建的。每次調(diào)用dispatch_semaphore_signal函數(shù),都會將信號量計數(shù)器加1。如果有線程在等待信號量,那么其中的一個線程會被解除阻塞,繼續(xù)執(zhí)行后續(xù)的代碼
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// 需要等待信號量
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
NSLog(@"Task completed.");
});
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// 執(zhí)行任務(wù)
NSLog(@"Task executing...");
// 增加信號量計數(shù)器的值,解除阻塞
dispatch_semaphore_signal(semaphore);
});
在上面的代碼中,第一個線程會等待信號量,直到第二個線程調(diào)用dispatch_semaphore_signal函數(shù)增加信號量計數(shù)器的值。這時,第一個線程就可以繼續(xù)執(zhí)行后續(xù)的代碼,輸出Task completed.的日志信息。