2

My app needs to download a batch of files with the following requirements:

  1. The batch should be processed somewhat in order (never at the expense of downloading files simultaneously).
  2. Downloads should continue in background if the app is closed.
  3. The user might trigger other downloads at any time. These downloads should have priority over the batch.
  4. Given that the user might trigger the download of any file in the batch, if a file is already downloaded, it shouldn't be downloaded again. I should be able to check if the file is already downloaded before starting the actual download, or remove it from the queue.

This can certainly be achieved with a lot of boilerplate code (too much to post here). What would be the simplest way to do this with iOS 7 brand new NSURLSession and/or AFNetworking 2.0?

hpique
  • 119,096
  • 131
  • 338
  • 476
  • I should note that this is a more specific case of this question: http://stackoverflow.com/questions/19513886/download-several-files-in-background-ios-7-only?rq=1 – hpique Nov 14 '13 at 14:19

1 Answers1

0

My shot would be:

  • Make an NSOperation subclass which handles just 1 download.
  • Setup 2 NSOperationQueues, a high priority queue and a low priority queue. You can also set different concurrent operations for each queue.

For each download you can add an operation in one of the queues.

The simplest way to download a file might not even be AFNetworking or NSURLSession, but that depends on your requirements. If you just need a plain downlaod you can use something like this:

NSData *downloadData = [NSData dataWithContentsOfURL:yourURL];
Leijonien
  • 1,415
  • 14
  • 16
  • Thanks @Leijonien. This doesn't address requirement 2. It is very similar to what I have now, and it requires a lot of code to deal with requirements 3 and 4. I was hoping that with AFNetworking or iOS 7 a simpler, less intrusive approach would be possible. – hpique Nov 14 '13 at 15:28
  • Sorry, didn't read that correctly. So AFNetworking is probably the best choice. Did you check this one? http://stackoverflow.com/questions/7800614/does-afnetworking-have-backgrounding-support – Leijonien Nov 14 '13 at 15:36
  • I don't think that's quite the same as NSURLSession's background configuration, which continues when the app is suspended. https://developer.apple.com/library/ios/documentation/cocoa/Conceptual/URLLoadingSystem/Articles/UsingNSURLSession.html#//apple_ref/doc/uid/TP40013509-SW44 – hpique Nov 14 '13 at 16:10