惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

V
V2EX
PCI Perspectives
PCI Perspectives
Webroot Blog
Webroot Blog
Help Net Security
Help Net Security
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Hacker News: Ask HN
Hacker News: Ask HN
Security Latest
Security Latest
P
Palo Alto Networks Blog
Spread Privacy
Spread Privacy
S
Securelist
V2EX - 技术
V2EX - 技术
Schneier on Security
Schneier on Security
P
Proofpoint News Feed
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Forbes - Security
Forbes - Security
N
News | PayPal Newsroom
Cyberwarzone
Cyberwarzone
C
Cisco Blogs
Cloudbric
Cloudbric
GbyAI
GbyAI
A
About on SuperTechFans
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Vercel News
Vercel News
P
Proofpoint News Feed
SecWiki News
SecWiki News
T
Tailwind CSS Blog
腾讯CDC
C
Cybersecurity and Infrastructure Security Agency CISA
The Hacker News
The Hacker News
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
M
MIT News - Artificial intelligence
爱范儿
爱范儿
Microsoft Azure Blog
Microsoft Azure Blog
T
Troy Hunt's Blog
G
Google Developers Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Cisco Talos Blog
Cisco Talos Blog
D
DataBreaches.Net
V
Vulnerabilities – Threatpost
博客园 - 叶小钗
C
Check Point Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Know Your Adversary
Know Your Adversary
T
Tor Project blog
Google DeepMind News
Google DeepMind News
F
Fortinet All Blogs
Y
Y Combinator Blog
H
Help Net Security
Latest news
Latest news

博客园 - 幻化成疯

今天又是一年的最后一个工作日 今天是最后一个工作日 过年了,龙年 新年又来了!今天是放假的最后前的最后一个工作日! Vue开发时运行npm install命令导致npm包自动升级而引起bug iOS AppIcon透明圆角导致的动画问题 iOS14 毛皮玻璃Blur效果英文教程推荐 iOS14 SceneKit Camera 的视角 Swift5 的 Enum iOS14 Core Location后台定位 用.Net 开发Windows Service ,合并dll到exe中 iOS SpriteKit 字体设置无效问题 2021又来到了! Dart 和 Javascript 中的await 和 async的理解 iOS12 判断网络链接状态NWPathMonitor 又离职了~ 留个纪念,过了这么多年,又干回Android了! mac上运行shell脚本遇到回车字符错误 - 幻化成疯 - 博客园 以iphone作为热点时ios程序中的UDP广播地址
iOS12 中的后台下载与上传
幻化成疯 · 2019-05-25 · via 博客园 - 幻化成疯

严格意义上来说,iOS并不能像Android一样,真的在后台开启一个下载Service,一直下载。但是它可以进行在系统允许范围内的后台上传和下载。
当使用

NSURLSessionConfiguration* sessionConfig = [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:identifier];
 NSURLSession *session = [NSURLSession sessionWithConfiguration:sessionConfig delegate:self delegateQueue:self.operationQueue];

创建一个Session后,由此Session创建出的NSURLSessionDownloadTask 和 NSURLSessionUploadTask 都会被交给系统的Background Transfer Services负责上传下载。 这种方法创建的task,不支持block形式的创建方法,必须使用比较原始的delegate回调方式。而且目前NSURLSessionDataTask对background模式的支持不好,切入后台后, 没有任何系统回调函数, 过一段时间再把程序切入前台直接报错。至于原因,有如下帖子说明:

https://stackoverflow.com/questions/39639268/ios-background-task-using-nsurlsessiondatatask

其中的主要意思是,NSURLSessionDataTask是把数据保存在内存中的,而Background Transfer Services无法跨进程读取内存数据。但是实际上,官方文档并没有说明DataTask不支持后台模式,很可能是目前的iOS版本里存在bug。

官方推荐的使用方法是在一个Session中,直接将需要处理的任务全部开启,程序进入后台,这些任务全部下载完成后,系统会通知程序。这种模式很适合下载的Url数量很少的情况,比如只需要下载几个url中的数据。但是如果需要下载几十个或是上百个url的资源,我们就不能一下子发起那么多的请求,服务器很可能无法及时响应,导致请求超时出错。那么想要达到后台下载的效果,就需要把众多请求分组放入多个Session,一个Session中的所有下载完成后,系统回产生回调,我们在系统回调中再次开启新的Session,继续下载。