GCD网络请求:无序(异步+并发)有序(异步+串行) 您所在的位置:网站首页 gcd代码 GCD网络请求:无序(异步+并发)有序(异步+串行)

GCD网络请求:无序(异步+并发)有序(异步+串行)

2023-05-15 05:54| 来源: 网络整理| 查看: 265

GCD:异步+并发(信号量)

requestNetworking要保证请求完成才可以再次请求加个判断:isFinishLoading

-(void)touchesBegan:(NSSet *)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); //第一个网络请求 dispatch_group_async(dispatchGroup, dispatchQueue, ^{ NSLog(@"--------group1--------%@",[NSThread currentThread]); [self test1]; }); //第二个网络请求 dispatch_group_async(dispatchGroup, dispatchQueue, ^{ NSLog(@"--------group2--------%@",[NSThread currentThread]); [self test2]; }); //第三个网络请求 dispatch_group_async(dispatchGroup, dispatchQueue, ^{ NSLog(@"--------group3--------%@",[NSThread currentThread]); [self test3]; }); //第四个网络请求 dispatch_group_async(dispatchGroup, dispatchQueue, ^{ NSLog(@"--------group4--------%@",[NSThread currentThread]); [self test4]; }); //第五个网络请求 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(@"第一步网络请求完成"); 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(@"第二步网络请求完成"); 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(@"第三步网络请求完成"); 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(@"第四步网络请求完成"); 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(@"第五步网络请求完成"); 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--------{number = 13, name = (null)} 2023-04-28 11:30:52.339833+0800 text_04[3758:109451] --------group2--------{number = 9, name = (null)} 2023-04-28 11:30:52.339854+0800 text_04[3758:109449] --------group3--------{number = 7, name = (null)} 2023-04-28 11:30:52.339864+0800 text_04[3758:109470] --------group4--------{number = 15, name = (null)} 2023-04-28 11:30:52.339890+0800 text_04[3758:109453] --------group5--------{number = 3, name = (null)} 2023-04-28 11:30:52.364842+0800 text_04[3758:109454] 第一步网络请求完成 2023-04-28 11:30:52.399705+0800 text_04[3758:109456] 第五步网络请求完成 2023-04-28 11:30:52.428851+0800 text_04[3758:109456] 第四步网络请求完成 2023-04-28 11:30:52.430836+0800 text_04[3758:109470] 第二步网络请求完成 2023-04-28 11:30:53.123856+0800 text_04[3758:109451] 第三步网络请求完成 2023-04-28 11:30:53.124068+0800 text_04[3758:109415] --------updateUi--------{number = 1, name = main}

在这个示例中,我们使用dispatch_group_async()函数将每个请求添加到dispatch_group_t中。对于每个请求,我们在一个dispatch_queue_t中执行它,并使用dispatch_semaphore_t在请求完成时释放信号量。我们在每个请求的末尾使用dispatch_semaphore_wait()函数等待信号量,以确保在处理完每个请求之前不会离开dispatch_group_t。最后,我们调用dispatch_group_notify()函数来指定所有请求完成后要执行的代码块。

在这个示例中,dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER):放在子线程,不会阻塞主线程,可以执行其他操作。

GCD:异步+并发(dispatch_group_enter)推荐用这个目前项目在使用

requestNetworking要保证请求完成才可以再次请求加个判断:isFinishLoading

-(void)touchesBegan:(NSSet *)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(); //第一个网络请求 dispatch_group_enter(self.group); dispatch_group_async(self.group, queue, ^{ NSLog(@"--------group1--------%@",[NSThread currentThread]); [self test1]; }); //第二个网络请求 dispatch_group_enter(self.group); dispatch_group_async(self.group, queue, ^{ NSLog(@"--------group2--------%@",[NSThread currentThread]); [self test2]; }); //第三个网络请求 dispatch_group_enter(self.group); dispatch_group_async(self.group, queue, ^{ NSLog(@"--------group3--------%@",[NSThread currentThread]); [self test3]; }); //第四个网络请求 dispatch_group_enter(self.group); dispatch_group_async(self.group, queue, ^{ NSLog(@"--------group4--------%@",[NSThread currentThread]); [self test4]; }); //第五个网络请求 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(@"第一步网络请求完成"); 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(@"第二步网络请求完成"); 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(@"第三步网络请求完成"); 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(@"第四步网络请求完成"); 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(@"第五步网络请求完成"); dispatch_group_leave(self.group); }]; [task resume]; } 2023-04-28 11:28:50.619371+0800 text_04[3663:105006] --------updateUi--------{number = 1, name = main} 2023-04-28 11:28:50.619658+0800 text_04[3663:105186] --------group1--------{number = 7, name = (null)} 2023-04-28 11:28:50.619718+0800 text_04[3663:105187] --------group2--------{number = 4, name = (null)} 2023-04-28 11:28:50.619749+0800 text_04[3663:105191] --------group3--------{number = 5, name = (null)} 2023-04-28 11:28:50.619763+0800 text_04[3663:105188] --------group4--------{number = 9, name = (null)} 2023-04-28 11:28:50.619794+0800 text_04[3663:105190] --------group5--------{number = 3, name = (null)} 2023-04-28 11:28:50.652971+0800 text_04[3663:105187] 第一步网络请求完成 2023-04-28 11:28:50.704591+0800 text_04[3663:105190] 第四步网络请求完成 2023-04-28 11:28:50.709022+0800 text_04[3663:105190] 第二步网络请求完成 2023-04-28 11:28:50.752817+0800 text_04[3663:105186] 第五步网络请求完成 2023-04-28 11:28:51.462473+0800 text_04[3663:105187] 第三步网络请求完成 2023-04-28 11:28:51.462692+0800 text_04[3663:105006] --------updateUi--------{number = 1, name = main}

在上面的代码中,我们首先创建了一个dispatch_group_t类型的组,并将它添加到全局队列中。然后,我们使用dispatch_group_enter函数进入组,并分别发起了三个异步网络请求,并在每个请求完成后打印出完成信息,并调用dispatch_group_leave函数以便离开组。最后,我们使用dispatch_group_notify函数在所有请求完成后打印出“所有异步网络请求已完成”的信息。

GCD:异步+串行(信号量,开子线程按顺序执行)

requestNetworking要保证请求完成才可以再次请求加个判断:isFinishLoading

-(void)touchesBegan:(NSSet *)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); //第一个网络请求 dispatch_group_async(dispatchGroup, dispatchQueue, ^{ NSLog(@"--------group1--------%@",[NSThread currentThread]); [self test1]; }); dispatch_semaphore_wait(self.semaphore, DISPATCH_TIME_FOREVER); //第二个网络请求 dispatch_group_async(dispatchGroup, dispatchQueue, ^{ NSLog(@"--------group2--------%@",[NSThread currentThread]); [self test2]; }); dispatch_semaphore_wait(self.semaphore, DISPATCH_TIME_FOREVER); //第三个网络请求 dispatch_group_async(dispatchGroup, dispatchQueue, ^{ NSLog(@"--------group3--------%@",[NSThread currentThread]); [self test3]; }); dispatch_semaphore_wait(self.semaphore, DISPATCH_TIME_FOREVER); //第四个网络请求 dispatch_group_async(dispatchGroup, dispatchQueue, ^{ NSLog(@"--------group4--------%@",[NSThread currentThread]); [self test4]; }); dispatch_semaphore_wait(self.semaphore, DISPATCH_TIME_FOREVER); //第五个网络请求 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(@"第一步网络请求完成"); 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(@"第二步网络请求完成"); 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(@"第三步网络请求完成"); 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(@"第四步网络请求完成"); 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(@"第五步网络请求完成"); dispatch_semaphore_signal(self.semaphore); }]; [task resume]; } 2023-04-28 15:20:02.338944+0800 text_04[2325:54425] --------group1--------{number = 7, name = (null)} 2023-04-28 15:20:02.356631+0800 text_04[2325:54425] 第一步网络请求完成 2023-04-28 15:20:02.356897+0800 text_04[2325:54422] --------group2--------{number = 9, name = (null)} 2023-04-28 15:20:02.439213+0800 text_04[2325:54425] 第二步网络请求完成 2023-04-28 15:20:02.439444+0800 text_04[2325:54421] --------group3--------{number = 3, name = (null)} 2023-04-28 15:20:03.513870+0800 text_04[2325:54421] 第三步网络请求完成 2023-04-28 15:20:03.514117+0800 text_04[2325:54421] --------group4--------{number = 3, name = (null)} 2023-04-28 15:20:03.552748+0800 text_04[2325:54421] 第四步网络请求完成 2023-04-28 15:20:03.552924+0800 text_04[2325:54425] --------group5--------{number = 7, name = (null)} 2023-04-28 15:20:03.685218+0800 text_04[2325:54421] 第五步网络请求完成 2023-04-28 15:20:03.685468+0800 text_04[2325:54236] --------updateUi--------{number = 1, name = main}

在上面的示例中,每个网络请求都是异步执行的,并按顺序执行。在每个网络请求执行完成后,都通过dispatch_semaphore_signal方法使dispatch_semaphore_t对象的计数器加1。在每个网络请求执行之前,都需要通过dispatch_semaphore_wait方法等待dispatch_semaphore_t对象的计数器达到相应的值。一旦所有请求完成,将会处理所有请求的结果。

在上面的示例中,dispatch_semaphore_wait(self.semaphore, DISPATCH_TIME_FOREVER):放在主线程,这个会阻塞主线程,其他操作执行不了。只有当执行完网络请求才能做其他事情

信号量

dispatch_semaphore_signal是GCD信号量函数之一,它的作用是增加信号量的计数器,从而解除由dispatch_semaphore_wait函数所阻塞的线程。

dispatch_semaphore_signal的参数是一个dispatch_semaphore_t对象,它是由dispatch_semaphore_create函数创建的。每次调用dispatch_semaphore_signal函数,都会将信号量计数器加1。如果有线程在等待信号量,那么其中的一个线程会被解除阻塞,继续执行后续的代码

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), ^{ // 执行任务 NSLog(@"Task executing..."); // 增加信号量计数器的值,解除阻塞 dispatch_semaphore_signal(semaphore); });

在上面的代码中,第一个线程会等待信号量,直到第二个线程调用dispatch_semaphore_signal函数增加信号量计数器的值。这时,第一个线程就可以继续执行后续的代码,输出Task completed.的日志信息。



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有