3

I have a requirement to download a number of files (around 500). I have an array containing all the urls of these files, I wanted to use NSURLSession so that i can support background downloading too.

I cant think of correct way to achieve this. If i am initiating next file download after one is completed then background downloading will not work.

shall I creating multiple downloading tasks and initiate?

Please suggest me how to achieve this.

3
  • You can use a Queue for this. Implement your own data manager, and delegate properties accordingly, and also implement queue functionally. Also I think NSURLConnection does already implement this. So, you can forget about all connections and directly ask NSURLConnection to lead all the files. Nov 22, 2013 at 12:24
  • Is there a compelling reason to use NSURLConnection? Or can you possibly use NSURLSession (>= iOS 7)? Nov 22, 2013 at 13:40
  • Sorry guys i was talking about NSURLSession(edited now). It supports background downloading but it is straight forward for downloading a large file, but my problem is about downloading a number of small/medium sized files.
    – Amit
    Nov 23, 2013 at 18:11

2 Answers 2

0

Edit: First, sorry for late response and here is a solution for your problem. Begin with downloading Apple's Simple Background Transfer sample. Then you will see the URLSessionDidFinishEventsForBackgroundURLSession method in view controller. You can modify this method for calling another download task like below sample and I think this is what you want to do.

There is also a comment over this method like this the session delegate will receive this message to indicate that all messages previously enqueued for this session have been delivered. So creating a queue for your requests could be better solution then this.

- (void)URLSessionDidFinishEventsForBackgroundURLSession:(NSURLSession *)session
{
    APLAppDelegate *appDelegate = (APLAppDelegate *)[[UIApplication sharedApplication] delegate];
    if (appDelegate.backgroundSessionCompletionHandler) {
        void (^completionHandler)() = appDelegate.backgroundSessionCompletionHandler;
        appDelegate.backgroundSessionCompletionHandler = nil;
        completionHandler();

        [self start:nil];
    }

    NSLog(@"All tasks are finished");
}
4
  • If you have a requirement downloading 500 files, doesn't this automatically imply that this operation MUST be cancelable, and resumable? Nov 22, 2013 at 13:38
  • If I have a requirement like this, yes it MUST be cancellable but he didn't mention anything about it. It can be a school project or he could be doing some benchmark tests. If he'll update his question with more specific needs, I can gladly edit my answer.
    – Boran
    Nov 22, 2013 at 14:09
  • please see the edits, my apologies for putting question wrongly. Resume and cancel will be part of the requirement but currently i am just looking for download solution.
    – Amit
    Nov 23, 2013 at 18:12
  • With this solution, the files will be downloaded sequentially, which might take a while for 500 files. Nov 23, 2013 at 18:34
0

I was having problems with this. My app had to update itself and download news videos in the background using BACKGROUND FETCH to get json list of files then firing off n number of webservice calls to download these files using BACKGROUND TRANSFER

[NSURLSessionConfiguration backgroundSessionConfiguration:

For each file I was creating one NSSession and one NSURLSessionDownloadTask.

file 1 - NSSession1 > NSURLSessionDownloadTask1
file 2 - NSSession2 > NSURLSessionDownloadTask2
file 3 - NSSession3 > NSURLSessionDownloadTask3

This woke fine when the app was in the foreground.

But I had problems when the app was in background and woken by BACKGROUND FETCH One file would download and then it would halt. It was like only the first NSSession1 was executed. It may have been that iOS was waiting till device was idle again to run next session but this was too slow

I got it working by having one NSSession and attaching all NSURLSessionDownloadTask3

 NSURLSession * backgroundSession_ =
for(url to call){
   create NSURLSessionDownloadTask1 (set its session:backgroundSession_)
   create NSURLSessionDownloadTask2 (set its session:backgroundSession_)
   create NSURLSessionDownloadTask3 (set its session:backgroundSession_)
}

Be careful when doing this call NSSession finishTasksAndInvalidate not invalidateAndCancel

  //[session invalidateAndCancel];
   [session finishTasksAndInvalidate];

invalidateAndCancel will stop the session and not finish the other download tasks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.