NSURLSessionDownloadTask automatically resumes the entire task in the background

I have a requirement to download files in sequential order. Currently, I can do this while the application is in the foreground.

Below is the logic I used.

  • Create all download tasks.

  • Resume one at a time and as the current one completes the resumption of the following from the URLSession:task:didCompleteWithError:

This logic works when the application is in the foreground, but as soon as the application starts to run in the background (crash) and we start the application again before the download is completed, all task states have been changed to resume, and they all load at the same time.

Is this the expected behavior or something that I am missing to order this in serial mode in the background?

Edit: I checked by creating a boot job one at a time. After completing the first task, create the next one inside setTaskDidCompleteBlock and so on. It completes only the first task, and after that the session crashed when the task was created inside setTaskDidCompleteBlock (this only happens when working in the background, for the priority of its work is excellent).

Here are my screenshots of the crash log:

drive.google.com/file/d/0B9jFCUPsPtV6YW5zbTJrQ0pQYlk/view?usp=sharing

and

drive.google.com/file/d/0B9jFCUPsPtV6UkEwOURpZmZYcEU/view?usp=sharing

Any help would be appreciated.

+1
ios nsurlsession nsurlsessiondownloadtask afnetworking-2
Aug 13 '15 at 16:32
source share
2 answers

If you absolutely need to run these queries sequentially, I would suggest not creating instances of all these tasks in advance, but rather creating them one at a time, but only creating an instance of the next one upon completion of the previous one.

But we must admit that you are paying a significant performance hit for query execution consistently. (And this problem will be increased when using background sessions.) If at all possible, see if you can change your requests to run at the same time. It’s clear that if you need the output of one to create a request for another, you are stuck (or at least until you reorganize the server code), but this is clearly not a problem here (because you created all the requests up front ) If you are doing this sequential query process for artificial reasons (for example, the code fills the array and you want it in order), you might want to redesign the implementation to remove this artificial restriction.

+1
Aug 14 '15 at 10:35
source share

I saw it too. If you create downloadTask when the application is in the foreground, but do not call resume() , it does not start - however, it will start automatically when the application is created.

The solution is to explicitly call suspend() for each downloadTask file when it is created. Then, when you are ready to start downloading it, call resume() .

Apparently, the newly created downloadTask does not pause or resume. Its initial state is not .Running , but when the application is embedded, it is shifted to .Running because it was not explicitly suspended. This is an amazing behavior; I don't know why this background session works this way.

0
Mar 21 '17 at 16:34
source share



All Articles