博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
网络请求顺序执行,顺序回调
阅读量:5907 次
发布时间:2019-06-19

本文共 3395 字,大约阅读时间需要 11 分钟。

  hot3.png

多个网络任务从0到9顺序执行,并且按顺序回调:

将所有操作顺序加入到队列中,执行操作.

将所有回调有序插入队列后执行;

@IBAction func clickAction(_ sender: Any) {        print("click \(Thread.current)")                /** 存放网络请求任务的队列 */        var requestList = Array
.init() /** 存放回调的队列 */ var cbList = Array
.init(repeating: BlockOperation(block: { print("default operation") }), count: 10) /** 记录网络请求有效回调的数量 */ var count = 0 for index in 0...9 { let operation = BlockOperation.init { print("request >> \(index) \(Thread.current)") var request = URLRequest.init(url: URL.init(string: "https://www.baidu.com")!) request.timeoutInterval = 5 let task = URLSession.shared.dataTask(with: request) { (data, response, error) in //回调操作 let op = BlockOperation.init(block: { print("callback -- \(index) -- \(Thread.current)") }) //将操作加入队列 cbList[index] = op count += 1 //所有回调加入队列后,执行回调 if count == 10 { self.doCallback(callbackList: cbList) } } task.resume() } //请求任务依次加入到队列中 requestList.append(operation) if index > 0 { let op1 = requestList[index-1] let op2 = requestList[index] op2.addDependency(op1) //使op2依赖op1 } } //执行请求 let queue = OperationQueue.init() queue.maxConcurrentOperationCount = -1 queue.addOperations(requestList, waitUntilFinished: false) } func doCallback(callbackList:Array
) -> Void { let cbQueue = OperationQueue.init() cbQueue.maxConcurrentOperationCount = 1 //设置为串行队列.(如果不需要顺序执行,可以设置为并行队列) cbQueue.addOperations(callbackList, waitUntilFinished: true) //所有回调执行完成后,通知主线程,故设置true OperationQueue.main.addOperation { print("update UI \(Thread.current)") } }

输出结果

click 
{number = 1, name = main}request >> 0
{number = 4, name = (null)}request >> 1
{number = 4, name = (null)}request >> 2
{number = 4, name = (null)}request >> 3
{number = 4, name = (null)}request >> 4
{number = 4, name = (null)}request >> 5
{number = 4, name = (null)}request >> 6
{number = 4, name = (null)}request >> 7
{number = 4, name = (null)}request >> 8
{number = 4, name = (null)}request >> 9
{number = 4, name = (null)}callback -- 0 --
{number = 6, name = (null)}callback -- 1 --
{number = 7, name = (null)}callback -- 2 --
{number = 7, name = (null)}callback -- 3 --
{number = 7, name = (null)}callback -- 4 --
{number = 7, name = (null)}callback -- 5 --
{number = 7, name = (null)}callback -- 6 --
{number = 6, name = (null)}callback -- 7 --
{number = 6, name = (null)}callback -- 8 --
{number = 7, name = (null)}callback -- 9 --
{number = 7, name = (null)}update UI
{number = 1, name = main}

 

转载于:https://my.oschina.net/Jerod/blog/1806738

你可能感兴趣的文章
Oracle:数据类型对应表
查看>>
洛谷P1349 广义斐波那契数列
查看>>
BZOJ3160 万径人踪灭
查看>>
Okhttp3请求网络开启Gzip压缩
查看>>
pycharm配置mysql数据库连接访问
查看>>
Spring源码学习:第0步--环境准备
查看>>
烂泥:rsync与inotify集成实现数据实时同步更新
查看>>
call & apply
查看>>
学习英语哦
查看>>
第六届蓝桥杯java b组第四题
查看>>
通过TortoiseGIT怎么把本地项目上传到GitHub
查看>>
Python 1 Day
查看>>
Python基础学习笔记(十:二进制位运算)
查看>>
C语言中字符串结束符
查看>>
技术工作者上升到思想,哲学层面也许更好
查看>>
LCD12864使用总结
查看>>
wireshark简明教程
查看>>
EditPlus配置Java编译器
查看>>
app已损坏,打不开。你应该将它移到废纸篓
查看>>
Switchover and Failover说明
查看>>