iOS基础类iOS网络

iOS 网络请求顺序处理(信号量,group)

2022-05-12  本文已影响0人  心猿意码_
一、日常开发中,会经常遇到这样的需求:
二、以上三种需求,分别在此列举下示例:
//创建时,值为<0返回null;值为0时,当前线程无法直接执行wait,需等待signal信号+1后执行wait
dispatch_semaphore_t sema = dispatch_semaphore_create(0); semaphore
//发送一个信号 +1
dispatch_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)loadListMenuWithFinishMark:(NSString *)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_waitdispatch_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)loadListMenuWithFinishMark:(NSString *)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] 开始====A
2022-05-12 10:29:55.706748+0800 SemaphoreDemo[12269:3284782] 结束======A
2022-05-12 10:29:55.706758+0800 SemaphoreDemo[12269:3284921] 开始====B
2022-05-12 10:29:55.712722+0800 SemaphoreDemo[12269:3284782] 结束======B
2022-05-12 10:29:55.712728+0800 SemaphoreDemo[12269:3284921] 开始====C
2022-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)loadListMenuWithFinishIndex:(NSInteger)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] 开始====0
2022-05-12 10:45:15.439159+0800 SemaphoreDemo[13910:3299720] 结束======0
2022-05-12 10:45:15.439178+0800 SemaphoreDemo[13910:3299813] 开始====1
2022-05-12 10:45:15.444384+0800 SemaphoreDemo[13910:3299720] 结束======1
2022-05-12 10:45:15.444405+0800 SemaphoreDemo[13910:3299813] 开始====2
2022-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)loadListFinishMark:(NSString *)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] 开始====A
2022-05-12 10:52:32.508026+0800 SemaphoreDemo[14680:3307180] 开始====B
2022-05-12 10:52:32.626408+0800 SemaphoreDemo[14680:3307180] 结束======B
2022-05-12 10:52:32.626604+0800 SemaphoreDemo[14680:3307180] 结束======A
2022-05-12 10:52:32.627107+0800 SemaphoreDemo[14680:3307180] 开始====C
2022-05-12 10:52:32.631006+0800 SemaphoreDemo[14680:3307180] 结束======C

码字不易,留个赞在走吧!!👍🏻👍🏻👍🏻

上一篇下一篇

猜你喜欢

热点阅读