Alamofire源码导读二:发起请求及内部加锁的逻辑

以创建一个 DataRequest 为例子

发起请求

创建 SessionManager

顺带也创建了一个 SessionDelegate
持有一个urlSession,持有一个串行的 DispatchQueue A
注意,这个不是urlSession 回调方法执行时所在的OperationQueue

创建 Requestable 的 struct,并创建underlying 的 URLSessionDataTask

目前不太清楚作用是什么,但是文档上的注释写着 Helper Types
持有一个 urlRequest
然后使用这个 Requestable,创建一个 URLSessionDataTask
注意要在SessionManager持有的串行队列中同步创建

sessionManager 创建一个 Request 对象

通过传入参数 URLSessionDataTaskurlSession
Request 会持有传入的 urlSession,并根据URLSessionDataTask,创建一个 TaskDelegate。 外部对这个TaskDelegate 的读取,被锁保护起来了。

/// The delegate for the underlying task.
open internal(set) var delegate: TaskDelegate {get {taskDelegateLock.lock() ; defer { taskDelegateLock.unlock() }return taskDelegate}set {taskDelegateLock.lock() ; defer { taskDelegateLock.unlock() }taskDelegate = newValue}
}

创建 TaskDelegate

新创建的 TaskDelegate 会持有传入的URLSessionDataTask.
在初始化方法中,会创建一个最大并发数是1的OperationQueue,并使之处于 suspend 状态。

sessionManger 持有 Request

创建 Request 之后,会把这个 Request 加到 sessionManger 持有的一个字典中,其读取方法也被加锁了。

var requests: [Int: Request] = [:]
private let lock = NSLock()/// Access the task delegate for the specified task in a thread-safe manner.
open subscript(task: URLSessionTask) -> Request? {get {lock.lock() ; defer { lock.unlock() }return requests[task.taskIdentifier]}set {lock.lock() ; defer { lock.unlock() }requests[task.taskIdentifier] = newValue}
}

处理网络数据

sessionDelegate 接受系统回调

比如方法urlSession(_, task:, didCompleteWithError:)中,会根据 URLSessionTask, 找到对应的 Request

运行 Request 所有的 validations

运行 TaskDelegate 的任务

所有的任务,都被加到了其持有的 OperationQueue 中。此时处于suspend 状态,要使其处于可运行的状态。
然后加到其中的所有任务,都会开始运行。

去掉对 Request 的持有

Request 已经收到并处理完了网络回调,因此就不必被 sessionDelegate 强持有了。
如果没有其他的持有者,Request 和其TaskDelegate 也会被释放。

其中的同步逻辑

sessionManager 的 DispatchQueue

仅用于创建 URLSessionTask 及部分文件目录操作,都是同步操作。
可能在任何线程创建 URLSessionTask

sessionDelegate 的 lock

仅用于对其持有的Request的读取进行加锁

Request 的 lock

仅对其持有的 TaskDelegate 的读取进行加锁

TaskDelegate 的串行 OperationQueue

其中的 Operation 在数据返回后会执行,并且不会并发。
各种 response 方法,都是在其中加入 Operation

TaskDelegate 的 lock

用于对 urlSessionTask 的读取进行加锁。

URLSessionTask 如何把整体串起来

  • sessionManager 中被创建
  • 初始化 Request 时被传入,用来创建TaskDelegate
  • TaskDelegate持有
  • sessionDelegate 中,其 taskIdentifier 被作为索引,来获取Request
  • 处理回调时,根据URLSessionTask,可以找到对应的Request,进行对应的处理。

转载于:https://www.cnblogs.com/huahuahu/p/Alamofire-yuan-ma-dao-du-er-fa-qi-qing-qiu-ji-nei-.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/278619.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

python os.path模块

os.path.abspath(path) #返回绝对路径os.path.basename(path) #返回文件名os.path.commonprefix(list) #返回list(多个路径)中,所有path共有的最长的路径。os.path.dirname(path) #返回文件路径os.path.exists(path) #路径存在则返回True,路径损坏返回Falseos.path…

让动画每次重复前都有延迟

动画不从0%开始即可 keyframes textmove {20% {transform: translateX(0);}100% {transform: translateX(-100%);} }

bixby映射软件下载_如何在Samsung Galaxy S8,S9,S10,Note 8或Note 9上重新映射Bixby按钮...

bixby映射软件下载We’ve said before, and we’ll say it again: Bixby sucks. You’re better off using Google Assistant any day of the week. The good news is, it’s now possible to remap the pointless Bixby button without using a third-party app. 我们之前已经…

JavaScript数据结构和算法

前言 在过去的几年中,得益于Node.js的兴起,JavaScript越来越广泛地用于服务器端编程。鉴于JavaScript语言已经走出了浏览器,程序员发现他们需要更多传统语言(比如C和Java)提供的工具。这些工具包括传统的数据结构&…

选择器

// 什么是 HTML 以及怎样使用 HTML 编写网页 // 网页的结构是怎样 // 什么是 CSS 以及怎样使用 CSS // 如何在网页中引入 JavaScript 代码 // 什么是 DOM, 常用 DOM API 使用 // document object model // application program interface // 什么是事件, 如何绑定事件 // // 应…

vue3打包后无法加载页面

1、配置输出路径 // vue.config.js module.exports {publicPath: ./ }2、不能使用history路由 // ... export default new Router({// mode: history, routes: [{path: /,name: home,component: Home}] })

如何使用Avira Rescue CD清洁感染的PC

When you’ve got a PC completely infected with viruses, sometimes it’s best to reboot into a rescue disc and run a full virus scan from there. Here’s how to use the Avira Rescue CD to clean an infected PC. 当您的PC完全感染了病毒时,有时最好重新…

汇编语言第二章总结

第二章 寄存器 (1) 字数据在寄存器中的存放 一个字由两个字节组成,可以存在一个16位寄存器中。 字的高8位 → 存放于通用寄存器的高8位寄存器 字的低8位 → 存放于通用寄存器的低8位寄存器。 例:十进制数据: 20000 → AX 对应的二进制…

Vue组件的三种调用方式

最近在写fj-service-system的时候,遇到了一些问题。那就是我有些组件,比如Dialog、Message这样的组件,是引入三方组件库,比如element-ui这样的,还是自己实现一个?虽然它们有按需引入的功能,但是…

axios把post的RequestPayload格式转为formdata

方法一:配置transformRequest,缺点:其他请求格式的数据也会被重新格式化(PUT,PATCH) const service axios.create({//设置axios为form-data 方法1// headers: {// post: {// "Content-T…

火狐打印预览_将打印和打印预览命令添加到Firefox的上下文菜单

火狐打印预览Have you been thinking about how much easier it would be to having the Print & Print Preview commands in Firefox’s Context Menu? The Print Context Menu extension for Firefox allows you to avoid having to use the File Menu to access the pr…

每个人都要在自己的“时区”里找到自己的快乐

祝小妹和自己生日快乐,人人都想快乐,却在平常的365天闷闷不乐,但愿家人朋友在平常的每一天都很够健康快乐! 在我那个开不了机的手机记事薄有句话还记得:你们不要刻意等我,因为可能现在的我还没来得及去发现…

《2017 云计算评测报告》:带你了解 AWS、阿里云、腾讯云等八家云计算服务提供商的综合用户体验情况...

报告电子版至听云官方博客下载:http://blog.tingyun.com/web/article/detail/1352 评测说明 评测目标:同一应用(网站)在不同云上的用户访问体验,以及对云资源的使用 洞察周期及范围:2017年4月-2017年9月 访…

js以变量为键

let key "dynamic",obj{[key]:true }; obj[key2]key console.log(obj)一般在配置文件中应用较多

搭建jenkins实现自动化部署

参考: https://www.cnblogs.com/rslai/p/8135460.html转载于:https://www.cnblogs.com/lihuanhuan/p/10612123.html

python 新闻摘要_每日新闻摘要:Microsoft内部禁止应用程序,这样就可以了

python 新闻摘要Recently, a list of apps that Microsoft prohibits for internal employee use leaked, including Slack, Grammarly, and others. It’s tempting to think these are the actions of a company hating competition, but the truth is more complicated. 最近…

vue使用process.env搭建自定义运行环境

一、vue-cli项目下默认有三种模式: development:在 vue-cli-service serve 时使用。production:在 vue-cli-service build 和 vue-cli-service test:e2e 时使用。test:在 vue-cli-service test:unit 时使用。 对应的 process.env…

bootstrap评分插件 Bootstrap Star Rating Examples

http://www.jq22.com/demo/bootstrap-star-rating-master201708041812/ 转载于:https://www.cnblogs.com/waw/p/8288951.html

http 请求报文

1、报文 2、http请求方法 restful接口 post:创建 put:更新 转载于:https://www.cnblogs.com/mengfangui/p/10171559.html

chrome硬件加速_如何在Chrome中打开和关闭硬件加速

chrome硬件加速Google Chrome comes equipped with hardware acceleration, a feature which takes advantage of your computer’s GPU to speed up processes and free vital CPU time. However, sometimes driver incompatibilities can cause this feature to misbehave an…