AFNetworking+GCD处理并发问题

我们在编程的时候会经常会出现这样的需求:同时请求几个接口回调成功以后在统一刷新UI,解决这个问题的方法有很多今天我们就说明下GCD下解决的方式。

一、GCD的leave和enter

我们利用dispatch_group_t创建队列组,手动管理group关联的block运行状态,进入和退出group的次数必须匹配。

//1.创建队列组  

    dispatch_group_t group = dispatch_group_create();  

//2.创建队列  

dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0);  

//3.添加请求  

    dispatch_group_async(group, queue, ^{  

        dispatch_group_enter(group);  

[HomeRequest getPointBuyAllConfigurationStrategyType:_dataType success:^(NSInteger code, NSDictionary *dict) {  

            dispatch_group_leave(group);  

} failuer:^(NSInteger code, NSString *message) {  

            dispatch_group_leave(group);  

        }];  

    });  

    dispatch_group_async(group, queue, ^{  

        dispatch_group_enter(group);  

[HomeRequest getStockLeverRiskStockCode:_buyingStrategyModel.stockCode strategyType:_dataType success:^(NSInteger code, NSDictionary *dict) {  

            dispatch_group_leave(group);  

} failuer:^(NSInteger code, NSString *message) {  

            dispatch_group_leave(group);  

        }];  

    });  

//4.队列组所有请求完成回调刷新UI  

    dispatch_group_notify(group, dispatch_get_main_queue(), ^{  

NSLog(@"model:%f",_buyingStrategyModel.leverrisk);  

    });  

GCD的信号量dispatch_semaphore_t

这种方式有点类似于通知模式,是利用监听信号量来发送消息以达到并发处理的效果,我们来看看代码:


二、创建信号量  

dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);  


dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0);  

    dispatch_group_t group = dispatch_group_create();  


    dispatch_group_async(group, queue, ^{  

[HomeRequest getPointBuyAllConfigurationStrategyType:_dataType success:^(NSInteger code, NSDictionary *dict) {  

            dispatch_semaphore_signal(semaphore);  

} failuer:^(NSInteger code, NSString *message) {  

            dispatch_semaphore_signal(semaphore);  

        }];  

    });  

    dispatch_group_async(group, queue, ^{  

[HomeRequest getStockLeverRiskStockCode:_buyingStrategyModel.stockCode strategyType:_dataType success:^(NSInteger code, NSDictionary *dict) {  

            dispatch_semaphore_signal(semaphore);  

} failuer:^(NSInteger code, NSString *message) {  

            dispatch_semaphore_signal(semaphore);  

        }];  

    });  

    dispatch_group_notify(group, queue, ^{  

        dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);  

        dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);  

NSLog(@"信号量为0");  

    });  

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 原文地址:http://blog.csdn.net/pianzhidenanren/article/details...
    默默_David阅读 513评论 0 0
  • 谈到iOS多线程,一般都会谈到四种方式:pthread、NSThread、GCD和NSOperation。其中,苹...
    攻城狮GG阅读 362评论 0 3
  • GCD (Grand Central Dispatch) :iOS4 开始引入,使用更加方便,程序员只需要将任务添...
    池鹏程阅读 1,441评论 0 2
  • Managing Units of Work(管理工作单位) 调度块允许您直接配置队列中各个工作单元的属性。它们还...
    edison0428阅读 8,230评论 0 1
  • 穷查理宝典的读后感: 第一,巴菲特演讲,让我认为世上很有名的人士都是先把自己推销出去。那么你首先得有一个好的演讲水...
    一把金沙子阅读 337评论 0 0

友情链接更多精彩内容