一、一样平常开发中,会经常碰到如许的需求:
- 网络哀求A返回乐成后,调用网络哀求B,网络哀求B效果返回乐成后调用网络哀求C(即:A—>B—>C)。
- 循环调用网络哀求A,确保每次哀求返回乐成后继承调用网络哀求A(即:A—>A—>A)。
- 网络哀求A和B返回乐成后,调用网路哀求C(即:A&B—>C)。
二、以上三种需求,分别在此枚举下示例:
//创建时,值为<0返回null;值为0时,当火线程无法直接实验wait,需等候signal信号+1后实验waitdispatch_semaphore_t sema = dispatch_semaphore_create(0); semaphore//发送一个信号 +1dispatch_semaphore_signal(sema);//等候信号(signal)>0实验dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);A—>B—>C:信号量处置惩罚
- (void)viewDidLoad { NSLog(@"开始====%@",@"A"); [self loadListMenuWithFinishMark"A"]; NSLog(@"开始====%@",@"B"); [self loadListMenuWithFinishMark"B"]; NSLog(@"开始====%@",@"C"); [self loadListMenuWithFinishMark"C"];}- (void)loadListMenuWithFinishMarkNSString *)mark{ // 创建信号量 dispatch_semaphore_t sema = dispatch_semaphore_create(0); NSString *urlString = @"http://www.qubaobei.com/ios/cf/dish_list.php?stage_id=1&limit=20&page=1"; [[AFHTTPSessionManager manager] GET:urlString parameters:nil headers:nil progress:^(NSProgress * _Nonnull downloadProgress) { } success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { // 发送信号 (+ 1) dispatch_semaphore_signal(sema); NSLog(@"结束======%@",mark); } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { dispatch_semaphore_signal(sema); NSLog(@"结束======%@", mark); }]; // 等候signal实验+1后(>0),才实验wait后的业务 dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);}打印效果(异常):
2022-05-12 10:35:50.871154+0800 SemaphoreDemo[12896:3290528] 开始====A缘故起因是:
如果在主线程运行以上代码,会发现没有效果输出;这是由于AFNetworking回调默认是主线程,如许dispatch_semaphore_wait和dispatch_semaphore_signal在同一个线程,如许就死锁了。
所以以上代码必要放在异步线程实验如下:
- (void)viewDidLoad { dispatch_async(dispatch_get_global_queue(0, 0), ^{ NSLog(@"开始====%@",@"A"); [self loadListMenuWithFinishMark"A"]; NSLog(@"开始====%@",@"B"); [self loadListMenuWithFinishMark"B"]; NSLog(@"开始====%@",@"C"); [self loadListMenuWithFinishMark"C"]; });}- (void)loadListMenuWithFinishMarkNSString *)mark{ // 创建信号量 dispatch_semaphore_t sema = dispatch_semaphore_create(0); NSString *urlString = @"http://www.qubaobei.com/ios/cf/dish_list.php?stage_id=1&limit=20&page=1"; [[AFHTTPSessionManager manager] GET:urlString parameters:nil headers:nil progress:^(NSProgress * _Nonnull downloadProgress) { } success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { // 发送信号 (+ 1) dispatch_semaphore_signal(sema); NSLog(@"结束======%@",mark); } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { dispatch_semaphore_signal(sema); NSLog(@"结束======%@", mark); }]; // 等候signal实验+1后(>0),才实验wait后的业务 dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);}打印效果:
2022-05-12 10:29:55.555207+0800 SemaphoreDemo[12269:3284921] 开始====A2022-05-12 10:29:55.706748+0800 SemaphoreDemo[12269:3284782] 结束======A2022-05-12 10:29:55.706758+0800 SemaphoreDemo[12269:3284921] 开始====B2022-05-12 10:29:55.712722+0800 SemaphoreDemo[12269:3284782] 结束======B2022-05-12 10:29:55.712728+0800 SemaphoreDemo[12269:3284921] 开始====C2022-05-12 10:29:55.715101+0800 SemaphoreDemo[12269:3284782] 结束======C循环哀求:信号量处置惩罚
- (void)viewDidLoad { [super viewDidLoad]; dispatch_async(dispatch_get_global_queue(0, 0), ^{ for (NSInteger i = 0; i < 3; i ++) { NSLog(@"开始====%ld",i); [self loadListMenuWithFinishIndex:i]; } });}- (void)loadListMenuWithFinishIndexNSInteger)index{ dispatch_semaphore_t sema = dispatch_semaphore_create(0); NSString *urlString = @"http://www.qubaobei.com/ios/cf/dish_list.php?stage_id=1&limit=20&page=1"; [[AFHTTPSessionManager manager] GET:urlString parameters:nil headers:nil progress:^(NSProgress * _Nonnull downloadProgress) { } success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { dispatch_semaphore_signal(sema); NSLog(@"结束======%ld",index); } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { dispatch_semaphore_signal(sema); NSLog(@"结束======%ld", index); }]; dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);}打印效果:
2022-05-12 10:45:15.329772+0800 SemaphoreDemo[13910:3299813] 开始====02022-05-12 10:45:15.439159+0800 SemaphoreDemo[13910:3299720] 结束======02022-05-12 10:45:15.439178+0800 SemaphoreDemo[13910:3299813] 开始====12022-05-12 10:45:15.444384+0800 SemaphoreDemo[13910:3299720] 结束======12022-05-12 10:45:15.444405+0800 SemaphoreDemo[13910:3299813] 开始====22022-05-12 10:45:15.464591+0800 SemaphoreDemo[13910:3299720] 结束======2多个网络哀求都完成之后举行某操纵(A&B—>C): Group处置惩罚
#import "AFNetworking.h"@interface ViewController ()@property (nonatomic,strong)dispatch_group_t group;@end@implementation ViewController- (void)viewDidLoad { [super viewDidLoad]; self.group = dispatch_group_create(); NSLog(@"开始====%@",@"A"); [self loadListFinishMark"A"]; NSLog(@"开始====%@",@"B"); [self loadListFinishMark"B"]; // 完成A&B后实验C哀求 dispatch_group_notify(self.group, dispatch_get_main_queue(), ^{ NSLog(@"开始====%@",@"C"); [self loadListFinishCMark]; });}#pragma mark - Group Request- (void)loadListFinishMarkNSString *)mark{ // enter 与 leave 逐一对应 dispatch_group_enter(self.group); NSString *urlString = @"http://www.qubaobei.com/ios/cf/dish_list.php?stage_id=1&limit=20&page=1"; [[AFHTTPSessionManager manager] GET:urlString parameters:nil headers:nil progress:^(NSProgress * _Nonnull downloadProgress) { } success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { NSLog(@"结束======%@",mark); dispatch_group_leave(self.group); } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { NSLog(@"结束======%@", mark); dispatch_group_leave(self.group); }];}- (void)loadListFinishCMark{ NSString *urlString = @"http://www.qubaobei.com/ios/cf/dish_list.php?stage_id=1&limit=20&page=1"; [[AFHTTPSessionManager manager] GET:urlString parameters:nil headers:nil progress:^(NSProgress * _Nonnull downloadProgress) { } success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { NSLog(@"结束======C"); } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { NSLog(@"结束======C"); }];}打印效果:
2022-05-12 10:52:32.505854+0800 SemaphoreDemo[14680:3307180] 开始====A2022-05-12 10:52:32.508026+0800 SemaphoreDemo[14680:3307180] 开始====B2022-05-12 10:52:32.626408+0800 SemaphoreDemo[14680:3307180] 结束======B2022-05-12 10:52:32.626604+0800 SemaphoreDemo[14680:3307180] 结束======A2022-05-12 10:52:32.627107+0800 SemaphoreDemo[14680:3307180] 开始====C2022-05-12 10:52:32.631006+0800 SemaphoreDemo[14680:3307180] 结束======C码字不易,留个赞在走吧!!?????? |