iOS:GCD【dispatch_semaphore】使用之所思

最近在使用GCD的时候,发现自己对dispatch_semaphore理解不是那么深刻,所以自己在网上找资料学习dispatch_semaphore,并对dispatch_semaphore有了一些自己的看法和理解。

1.异步任务使用semaphore

    NSLog(@"----------------------------开始----------------------------  线程:%@",[NSThread currentThread]);
    dispatch_semaphore_t sema =  dispatch_semaphore_create(0);
    
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        NSLog(@"---------------------------- 任务一开始 ---------------------------- 线程:%@",[NSThread currentThread]);
        [NSThread sleepForTimeInterval:2.0];
        NSLog(@"---------------------------- 任务一结束 ---------------------------- 线程:%@",[NSThread currentThread]);
        dispatch_semaphore_signal(sema);
    });
    dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
    
    
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        NSLog(@"---------------------------- 任务二开始 ---------------------------- 线程:%@",[NSThread currentThread]);
        [NSThread sleepForTimeInterval:2.0];
        NSLog(@"---------------------------- 任务二结束 ---------------------------- 线程:%@",[NSThread currentThread]);
        dispatch_semaphore_signal(sema);
    });
    dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
    
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        NSLog(@"---------------------------- 任务三开始 ---------------------------- 线程:%@",[NSThread currentThread]);
        [NSThread sleepForTimeInterval:2.0];
        NSLog(@"---------------------------- 任务三结束 ---------------------------- 线程:%@",[NSThread currentThread]);
        dispatch_semaphore_signal(sema);
    });
    dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
    
    NSLog(@"----------------------------结束---------------------------- 线程:%@",[NSThread currentThread]);

打印结果如下:

2018-11-20 10:05:48.165624+0800 Demo[2920:724653] ----------------------------开始----------------------------  线程:<NSThread: 0x282c6b180>{number = 1, name = main}
2018-11-20 10:05:48.166227+0800 Demo[2920:724705] ---------------------------- 任务一开始 ---------------------------- 线程:<NSThread: 0x282cebc40>{number = 3, name = (null)}
2018-11-20 10:05:50.171470+0800 Demo[2920:724705] ---------------------------- 任务一结束 ---------------------------- 线程:<NSThread: 0x282cebc40>{number = 3, name = (null)}
2018-11-20 10:05:50.171953+0800 Demo[2920:724705] ---------------------------- 任务二开始 ---------------------------- 线程:<NSThread: 0x282cebc40>{number = 3, name = (null)}
2018-11-20 10:05:52.177195+0800 Demo[2920:724705] ---------------------------- 任务二结束 ---------------------------- 线程:<NSThread: 0x282cebc40>{number = 3, name = (null)}
2018-11-20 10:05:52.177657+0800 Demo[2920:724710] ---------------------------- 任务三开始 ---------------------------- 线程:<NSThread: 0x282ce9380>{number = 4, name = (null)}
2018-11-20 10:05:54.182973+0800 Demo[2920:724710] ---------------------------- 任务三结束 ---------------------------- 线程:<NSThread: 0x282ce9380>{number = 4, name = (null)}
2018-11-20 10:05:54.183224+0800 Demo[2920:724653] ----------------------------结束---------------------------- 线程:<NSThread: 0x282c6b180>{number = 1, name = main}

这里我们可以分析到,semaphore确实起到了阻塞等待的作用。dispatch_semaphore_t sema = dispatch_semaphore_create(0)首先创建了一个信号量为0的semaphore,接着开启了一个异步任务,在任务中dispatch_semaphore_signal(sema)会使信号量+1,然后在主线程中dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER)使信号量-1,如果此时信号量为0,那么会一直等待。所以主线程会一直等待,直到异步任务执行完毕dispatch_semaphore_signal(sema)这里,对信号量+1,然后才会接着往下走。后面的任务以此类推。

可是在我们平常的使用中,更多是配合网络请求。例如请求B的数据依赖于请求A,只有先拿到了请求A的数据,才能请求B,当然这个需求解决办法有很多,但这里我们就只谈谈怎么用semaphore来解决这个需求。接着我们利用相同思路,结合AFN使用semaphore:

NSLog(@"----------------- 测试开始 -----------------");
    NSDictionary *dict = @{ @"page": @1,
                            @"pageSize": @20};
    
    dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
    //对AFN的简单封装 请求一个post任务
    [[CYW_NetworkingManager shareManager] cyw_networkType:NetWorkTypePOST
                                            withURLString:@"https://api.it120.cc/tz/shop/goods/list" withParameters:dict
                                         withSuccessBlock:^(id result) {
                                             NSLog(@"----------------- 任务一完成 -----------------%@ -----------------",[NSThread currentThread]);
                                             dispatch_semaphore_signal(semaphore);
                                             
                                         } withFailBlock:^(NSError *error) {
                                             dispatch_semaphore_signal(semaphore);
                                         }];
    dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
    //请求一个get任务
    [[CYW_NetworkingManager shareManager] cyw_networkType:NetWorkTypeGET withURLString:@"https://api.it120.cc/tz/shop/goods/category/all" withParameters:nil withSuccessBlock:^(id result) {
        
        NSLog(@"----------------- 任务二完成 ----------------- %@ -----------------",[NSThread currentThread]);
        dispatch_semaphore_signal(semaphore);

    } withFailBlock:^(NSError *error) {
        dispatch_semaphore_signal(semaphore);
    }];
    dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
 
    NSLog(@"----------------- 测试结束 -----------------");

打印结果如下:

2018-11-20 10:05:01.794640+0800 Demo[2918:724320] ----------------- 测试开始 -----------------

这里我们发现,仅仅只打印了测试开始,后面的任务都没有执行。那么究竟是什么原因,导致了主线程的阻塞呢?原来在AFN中,AFN已经已经将回调回到了主线程中:

  [[CYW_NetworkingManager shareManager] cyw_networkType:NetWorkTypeGET withURLString:@"https://api.it120.cc/tz/shop/goods/category/all" withParameters:nil withSuccessBlock:^(id result) {
        NSLog(@"----------------- 当前线程:%@ -----------------",[NSThread currentThread]);

    } withFailBlock:^(NSError *error) {
        
    }];

打印结果如下:
2018-11-20 10:11:32.721403+0800 Demo[2924:725812] ----------------- 当前线程:<NSThread: 0x283e4fa00>{number = 1, name = main} -----------------

而在执行到第一个dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER)时候,信号量就为0了,将主线程阻塞了,就没有办法继续往下执行dispatch_semaphore_signal(semaphore)。那为什么上面第一种方法可以执行呢?是因为上面的任务都是异步的,所以就算主线程阻塞了,在异步任务中还是可以执行dispatch_semaphore_signal(semaphore),也就不存在阻塞问题了。
那么我们怎么去解决这样的问题呢?这里我们就需要用到GCD的另外一种使用方式了:dispatch_group。对dispatch_group不是很了解的小伙伴可以参考这里,里面对GCD的各种使用方法都有很详细的讲解。
好了,废话不多说,接下来我们看看怎么用dispatch_group完成dispatch_semaphore和AFN的结合使用:

2.结合AFN使用dispatch_semaphore

NSLog(@"----------------- 测试开始 -----------------");
    dispatch_group_t gruop = dispatch_group_create();
    dispatch_group_async(gruop, dispatch_get_global_queue(0, 0), ^{
        dispatch_semaphore_t sema = dispatch_semaphore_create(0);
        NSLog(@"----------------- gruop:%@ -----------------",[NSThread currentThread]);
        
        [[CYW_NetworkingManager shareManager] cyw_networkType:NetWorkTypeGET withURLString:@"https://api.it120.cc/tz/shop/goods/category/all" withParameters:nil withSuccessBlock:^(id result) {
            
            NSLog(@"----------------- 任务一完成 ----------------- %@ -----------------",[NSThread currentThread]);
            dispatch_semaphore_signal(sema);
            
        } withFailBlock:^(NSError *error) {
            dispatch_semaphore_signal(sema);
        }];
        dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
        
        
        [[CYW_NetworkingManager shareManager] cyw_networkType:NetWorkTypeGET withURLString:@"https://api.it120.cc/tz/shop/goods/category/all" withParameters:nil withSuccessBlock:^(id result) {
            
            NSLog(@"----------------- 任务二完成 ----------------- %@ -----------------",[NSThread currentThread]);
            dispatch_semaphore_signal(sema);
            
        } withFailBlock:^(NSError *error) {
            dispatch_semaphore_signal(sema);
        }];
        dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
        
        
        [[CYW_NetworkingManager shareManager] cyw_networkType:NetWorkTypeGET withURLString:@"https://api.it120.cc/tz/shop/goods/category/all" withParameters:nil withSuccessBlock:^(id result) {
            
            NSLog(@"----------------- 任务三完成 ----------------- %@ -----------------",[NSThread currentThread]);
            dispatch_semaphore_signal(sema);
            
        } withFailBlock:^(NSError *error) {
            dispatch_semaphore_signal(sema);
        }];
        dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
        
    });
    
    dispatch_group_notify(gruop, dispatch_get_main_queue(), ^{
       NSLog(@"----------------- 测试结束 -----------------");
    });

打印结果如下:

2018-11-20 10:31:07.634295+0800 Demo[2942:730510] ----------------- 测试开始 -----------------
2018-11-20 10:31:07.636867+0800 Demo[2942:730582] ----------------- gruop:<NSThread: 0x283c8ebc0>{number = 3, name = (null)} -----------------
2018-11-20 10:31:08.057232+0800 Demo[2942:730510] ----------------- 任务一完成 ----------------- <NSThread: 0x283c01b40>{number = 1, name = main} -----------------
2018-11-20 10:31:08.130937+0800 Demo[2942:730510] ----------------- 任务二完成 ----------------- <NSThread: 0x283c01b40>{number = 1, name = main} -----------------
2018-11-20 10:31:08.229891+0800 Demo[2942:730510] ----------------- 任务三完成 ----------------- <NSThread: 0x283c01b40>{number = 1, name = main} -----------------
2018-11-20 10:31:08.230509+0800 Demo[2942:730510] ----------------- 测试结束 -----------------

根据打印的结果,这里完美解决了A任务、B任务之间的依赖关系的需求。也再次证明AFN的回调确实在主线程中(AFN的源码中,也可以找到,有兴趣的小伙伴可以研究下源码)。
那么,为什么用到dispatch_group可以解决问题呢?这里dispatch_group_async(gruop, dispatch_get_global_queue(0, 0)开辟一个新的子线程。而在子线程中,dispatch_semaphore_t sema = dispatch_semaphore_create(0)创建了一个信号量为0的semaphore,紧接着在当前子线程中执行dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER),那么会阻塞当前线程,一直等待,直到等待AFN的回调中dispatch_semaphore_signal(sema)将信号量+1。

如果对以上理解了的话,同样开启一个异步任务,也可以将任务一、二、三按顺序完成:

    NSLog(@"----------------- 测试开始 -----------------");
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        
        dispatch_semaphore_t sema = dispatch_semaphore_create(0);
        
        [[CYW_NetworkingManager shareManager] cyw_networkType:NetWorkTypeGET withURLString:@"https://api.it120.cc/tz/shop/goods/category/all" withParameters:nil withSuccessBlock:^(id result) {
            
            NSLog(@"----------------- 任务一完成 ----------------- %@ -----------------",[NSThread currentThread]);
            dispatch_semaphore_signal(sema);
            
        } withFailBlock:^(NSError *error) {
            dispatch_semaphore_signal(sema);
        }];
        dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
        
        
        [[CYW_NetworkingManager shareManager] cyw_networkType:NetWorkTypeGET withURLString:@"https://api.it120.cc/tz/shop/goods/category/all" withParameters:nil withSuccessBlock:^(id result) {
            
            NSLog(@"----------------- 任务二完成 ----------------- %@ -----------------",[NSThread currentThread]);
            dispatch_semaphore_signal(sema);
            
        } withFailBlock:^(NSError *error) {
            dispatch_semaphore_signal(sema);
        }];
        dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
        
        
        [[CYW_NetworkingManager shareManager] cyw_networkType:NetWorkTypeGET withURLString:@"https://api.it120.cc/tz/shop/goods/category/all" withParameters:nil withSuccessBlock:^(id result) {
            
            NSLog(@"----------------- 任务三完成 ----------------- %@ -----------------",[NSThread currentThread]);
            dispatch_semaphore_signal(sema);
            
        } withFailBlock:^(NSError *error) {
            dispatch_semaphore_signal(sema);
        }];
        dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
        
    });
    NSLog(@"----------------- 测试结束 -----------------");


打印结果如下:

2018-11-20 10:38:48.473069+0800 Demo[2950:732087] ----------------- 测试开始 -----------------
2018-11-20 10:38:48.473251+0800 Demo[2950:732087] ----------------- 测试结束 -----------------
2018-11-20 10:38:48.917159+0800 Demo[2950:732087] ----------------- 任务一完成 ----------------- <NSThread: 0x282351b40>{number = 1, name = main} -----------------
2018-11-20 10:38:49.005818+0800 Demo[2950:732087] ----------------- 任务二完成 ----------------- <NSThread: 0x282351b40>{number = 1, name = main} -----------------
2018-11-20 10:38:49.092094+0800 Demo[2950:732087] ----------------- 任务三完成 ----------------- <NSThread: 0x282351b40>{number = 1, name = main} -----------------

这里可以看到,虽然并不像使用dispatch_gruop那样,将调度组中的任务,执行完毕后再去调用
dispatch_group_notify里面的任务。但任务一、二、三却也是按顺序执行的。

以上是我个人对dispatch_semaphore的一点点理解,有错误的地方希望大家能指出来。

最后,个人还有点小疑问。既然AFN的回调已经在主线程中了,那么为什么我们还要AFN的回调中用dispatch_get_main_queue()回到主线程更新UI呢?

    原文作者:YW_Drenched
    原文地址: https://www.jianshu.com/p/62d7d9fd71ef
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞