简体   繁体   English

ios URLSession后台下载FIFO机制

[英]ios URLSession background download FIFO mechanism

I have an application which would support downloading the content to local disk. 我有一个支持将内容下载到本地磁盘的应用程序。 Users can choose the items they want to save. 用户可以选择他们想要保存的项目。 After downloading complete, I will unzip the downloaded file and encrypt then save to local. 下载完成后,我将解压缩下载的文件并加密,然后保存到本地。 I use NSURLSession with backgroundConfiguration to support background download. 我将NSURLSessionbackgroundConfiguration一起使用以支持后台下载。 I want user to access the downloaded content ASAP, so I implement my own queue to handle the download items. 我希望用户尽快访问下载的内容,因此我实现了自己的队列来处理下载项目。 I wish the download mechanism could work both on foreground and background. 我希望下载机制可以同时在前台和后台运行。 Here are some mechanisms and their result 以下是一些机制及其结果

  • Method 1: 方法1:

Create each download task first, and enqueue the object 首先创建每个下载任务,然后排队对象

downloadObj.downloadTask = [session downloadTaskWithRequest:request];

downloadObj.taskIdentifier = downloadObj.downloadTask.taskIdentifier;

[Queue enqueue:downloadObj];

Process the head object in the queue 处理队列中的头对象

obj = [Queue objectAtIndex:0];
[obj.downloadTask resume];

Handle next object in URLSession delegate function 处理URLSession委托函数中的下一个对象

-(void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)downloadURL {

   finishObj = [Queue findObjFromIdentifier:downloadTask.taskIdentifier];

   nextObj = [Queue findNextObj:finishObj];

   [nextObj.downloadTask resume];

   [Queue removeObject:finishObj];

}

This method could work properly when app always in foreground. 当应用始终位于前台时,此方法可以正常工作。 When the app enter background, all the created downloadTasks seems to resume automatically. 当应用进入后台时,所有创建的downloadTasks似乎都会自动恢复。 So they will share the bandwidth at the same time. 因此,它们将同时共享带宽。 It doesn't follow FIFO .... 它不遵循FIFO ....

  • Method 2: 方法2:

Create the download task in URLSession delegate function, and resume directly 在URLSession委托函数中创建下载任务,然后直接继续

This method only download the running download task when app has already entered background. 此方法在应用程序已进入后台时下载正在运行的下载任务

Anyone can give me some advise about the background with First In First Out property? 任何人都可以通过先进先出属性给我一些有关背景的建议吗?

You can't keep NSURLSessionTask objects around like that without starting them. 如果不启动NSURLSessionTask对象,就不能像这样保持对象。 The assumption is that you create the object, tweak its settings, and start it immediately. 假设您创建对象,调整其设置并立即启动它。 As a result, certain timeouts start counting down as soon as you create the object, IIRC. 结果,一旦创建对象IIRC,某些超时就会开始递减计数。 So if you create the object and then start it a minute or two later, it will time out before it even starts. 因此,如果您创建该对象,然后在一两分钟后启动它,它甚至会在启动之前超时。 I'm pretty sure that this fact is not mentioned in any of the documentation, unfortunately. 我很确定,不幸的是,任何文档中都没有提到这一事实。

A much better (read "likely to work") approach is to store the NSURLRequest object, and enqueue that. 一种更好的方法(请阅读“可能可行”)是存储NSURLRequest对象,并将其入队。 Then, at the appropriate time, dequeue the request, create a task, and start executing it. 然后,在适当的时间,使请求出队,创建任务,然后开始执行它。

Also, URL sessions aren't anywhere close to FIFO unless you limit the number of concurrent connections to 1. Otherwise, you'll hit the maximum number of concurrent requests per host, and it won't start any more requests for that host until one finishes, but requests for other hosts will start immediately. 另外,除非将并发连接数限制为1,否则URL会话不会接近FIFO。否则,您将达到每个主机的最大并发请求数,并且直到该主机再启动任何请求为止一个完成,但是对其他主机的请求将立即开始。

I think what you need is to set the httpMaximumConnectionsPerHost on URLSessionConfiguration object to 1. That means session will make one request at the time so it would be FIFO as you call it. 我想你需要的是设置httpMaximumConnectionsPerHostURLSessionConfiguration对象1.这意味着会议将当时一个请求所以这将是FIFO为你怎么称呼它。 There wouldn't even be a need to store and resume the next task in the delegate callback method, it would all happen automatically. 甚至不需要在委托回调方法中存储和恢复下一个任务,这将自动发生。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM