IOS中多个网络请求问题
来源:互联网 发布:julius it 编辑:程序博客网 时间:2024/06/05 11:31
在多线程中,有时候我们会遇到一个界面同时有多个网络请求(比如a,b,c,d四个网络请求),在这四个个请求结束后,在请求到数据去做其他操作(UI更新等)
在IOS的多线程解决方案中,GCD为我们提供了一种解决方案:dispatch_group 和信号量
//创建信号量
dispatch_semaphore_tsemaphore =dispatch_semaphore_create(0);
//创建一个队列组
dispatch_group_tgrouch =dispatch_group_create();
//创建全局队列,四个请求同时发起
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
//创建串行队列,四个请求依次执行
//dispatch_queue_tqueue =dispatch_queue_create("queues",NULL);
dispatch_group_async(grouch, queue, ^{
NSLog(@"网络请求a");
dispatch_semaphore_signal(semaphore);//释放通知
});
dispatch_group_async(grouch, queue, ^{
NSLog(@"网络请求b");
dispatch_semaphore_signal(semaphore);
});
dispatch_group_async(grouch, queue, ^{
NSLog(@"网络请求c");
dispatch_semaphore_signal(semaphore);
});
dispatch_group_async(grouch, queue, ^{
NSLog(@"网络请求d");
dispatch_semaphore_signal(semaphore);
});
//在所有请求都结束后,接到通知执行下面的方法
dispatch_group_notify(grouch, queue, ^{
//发起等待
dispatch_semaphore_wait(semaphore,DISPATCH_TIME_FOREVER);
//发起等待
dispatch_semaphore_wait(semaphore,DISPATCH_TIME_FOREVER);
//发起等待
dispatch_semaphore_wait(semaphore,DISPATCH_TIME_FOREVER);
//发起等待
dispatch_semaphore_wait(semaphore,DISPATCH_TIME_FOREVER);
NSLog(@"4个请求结束之后,可以在这里操作请求的数据,或者更新UI");
});
- IOS中多个网络请求问题
- ios 网络请求问题
- ios网络请求3840错误的问题
- iOS多个网络请求问题
- ios网络请求
- iOS网络请求
- ios网络异步请求
- iOS网络请求
- IOS 异步 网络请求
- IOS 同步 网络请求
- iOS中的网络请求
- ios学习--网络请求
- iOS中的网络请求
- iOS 网络请求
- IOS-网络请求
- iOS网络数据请求
- ios 网络请求框架
- IOS网络请求
- golang语言并发与并行——goroutine和channel的详细理解(一)
- poj 1753 Flip Game(状压DP)
- springmvc 数据传输跨域问题 access control allow origin——JSONP数据格式解决
- java的异常处理
- Linux中最危险的是个命令(很有趣呦~)(链接:http://os.51cto.com/art/201408/448756.htm)
- IOS中多个网络请求问题
- 多线程的实现方法
- (1)创建解决方案项目
- iOS学习笔记-050.Modal模态
- mybatis高级查询-一对一查询
- neuraltalk2将eval.lua改成一个服务 不断的增量分析图像
- 算法课第2周第2题——241. Different Ways to Add Parentheses
- intellij live Templates 设置
- 【数组】Leetcode编程题解:289. Game of Life Add to List