You can have multiple NSURLSessionDownloadTask using the same NSSession, and each of them runs one after another in the same mainQueue.
upon successful download, they call:
- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location
If you download MP4 from 100 mb and pic. 10 KB, then they are returned in different orders in this method.
So, to keep track of which session and which downloadTask are back in this
you need to set the row id before you call the web service
To distinguish / track NSURLSessions , you set
session.configuration.identifier
to highlight NSURLSessionDownloadTask , use
downloadTask_.taskDescription downloadTask_.taskDescription = [NSString stringWithFormat:@"%@",urlSessionConfigurationBACKGROUND_.identifier];
For example, in my project, I uploaded several of my favorite user videos and their thumbnails.
Each element had an identifier eg1234567 so I needed to make two calls for each loved one
so I created two identifiers
"1234567_VIDEO" "1234567_IMAGE"
then two ws calls are called and passed in the identifier to session.configuration.identifier
http://my.site/getvideo/1234567 "1234567_VIDEO" http://my.site1/getimage/1234567 "1234567_IMAGE"
iOS7 will load items in the background, the application may return to sleep mode Upon completion, it calls
- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location
Then i get
session.configuration.identifier "1234567_IMAGE"
separate it and check the values
1234567_IMAGE "1234567" "_IMAGE" > item at location is a MP4 so save as /Documents/1234567.mp4 "_VIDEO" > item at location is a jpg so save as /Documents/1234567.jpg
If you have 3 URLs, you can have one NSURLSessionDownloadTask for NSSession
file 1 - NSSession1 > NSURLSessionDownloadTask1 file 2 - NSSession2 > NSURLSessionDownloadTask2 file 3 - NSSession3 > NSURLSessionDownloadTask3
It looks like the application is in the foreground. But I had problems when using BACKGROUND TRANSFER with BACKGROUND FETCH. The first NSSession> NSURLSessionDownloadTask1 will return, and then none of the others will be called.
It is safer to have multiple NSURLSessionDownloadTask in one NSSession1
file 1 - NSSession1 > NSURLSessionDownloadTask1 file 2 - > NSURLSessionDownloadTask2 file 3 - > NSURLSessionDownloadTask3
Be careful when making this call to NSSession finishTasksAndInvalidate not invalidateAndCancel
invalidateAndCancel will terminate the session and not complete other load tasks