title: iOS Swift download lots of small files in background
body: <p>In my app I need to download files with the following requirements:</p>
  
  <ul>
  <li>download lots (say 3000) of small PNG files (say 5KB)</li>
  <li>one by one</li>
  <li>continue the download if the app in background</li>
  <li>if an image download fails (typically because the internet connection has been lost), wait for X seconds and retry. If it fails Y times, then consider that the download failed.</li>
  <li>be able to set a delay between each download to reduce the server load</li>
  </ul>
  
  <p>Is iOS able to do that? I'm trying to use NSURLSession and NSURLSessionDownloadTask, without success (I'd like to avoid starting the 3000 download tasks at the same time).</p>
  
  <p>EDIT: some code as requested by MwcsMac:</p>
  
  <p>ViewController:</p>
  
  <pre><code>class ViewController: UIViewController, URLSessionDelegate, URLSessionDownloadDelegate {
  
      // --------------------------------------------------------------------------------
      // MARK: Attributes
  
      lazy var downloadsSession: URLSession = {
  
          let configuration = URLSessionConfiguration.background(withIdentifier:"bgSessionConfigurationTest");
          let session = URLSession(configuration: configuration, delegate: self, delegateQueue:self.queue);
  
          return session;
      }()
  
      lazy var queue:OperationQueue = {
  
          let queue = OperationQueue();
          queue.name = "download";
          queue.maxConcurrentOperationCount = 1;
  
          return queue;
      }()
  
      var activeDownloads = [String: Download]();
  
      var downloadedFilesCount:Int64 = 0;
      var failedFilesCount:Int64 = 0;
      var totalFilesCount:Int64 = 0;
  
      // --------------------------------------------------------------------------------
  
  
  
      // --------------------------------------------------------------------------------
      // MARK: Lifecycle
  
      override func viewDidLoad() {
  
          super.viewDidLoad()
  
          startButton.addTarget(self, action:#selector(onStartButtonClick), for:UIControlEvents.touchUpInside);
  
          _ = self.downloadsSession
          _ = self.queue
      }
  
      // --------------------------------------------------------------------------------
  
  
  
      // --------------------------------------------------------------------------------
      // MARK: User interaction
  
      @objc
      private func onStartButtonClick() {
  
          startDownload();
      }
  
      // --------------------------------------------------------------------------------
  
  
  
      // --------------------------------------------------------------------------------
      // MARK: Utils
  
      func startDownload() {
  
          downloadedFilesCount = 0;
          totalFilesCount = 0;
  
          for i in 0 ..&lt; 3000 {
  
              let urlString:String = "http://server.url/\(i).png";
              let url:URL = URL(string: urlString)!;
  
              let download = Download(url:urlString);
              download.downloadTask = downloadsSession.downloadTask(with: url);
              download.downloadTask!.resume();
              download.isDownloading = true;
              activeDownloads[download.url] = download;
  
              totalFilesCount += 1;
          }
      }
  
      // --------------------------------------------------------------------------------
  
  
  
      // --------------------------------------------------------------------------------
      // MARK: URLSessionDownloadDelegate
  
      func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) {
  
          if(error != nil) { print("didCompleteWithError \(error)"); }
  
          failedFilesCount += 1;
      }
  
      func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didFinishDownloadingTo location: URL) {
  
          if let url = downloadTask.originalRequest?.url?.absoluteString {
  
              activeDownloads[url] = nil
          }
  
          downloadedFilesCount += 1;
  
          [eventually do something with the file]
  
          DispatchQueue.main.async {
  
              [update UI]
          }
  
          if(failedFilesCount + downloadedFilesCount == totalFilesCount) {
  
              [all files have been downloaded]
          }
      }
  
      // --------------------------------------------------------------------------------
  
  
  
      // --------------------------------------------------------------------------------
      // MARK: URLSessionDelegate
  
      func urlSessionDidFinishEvents(forBackgroundURLSession session: URLSession) {
  
          if let appDelegate = UIApplication.shared.delegate as? AppDelegate {
  
              if let completionHandler = appDelegate.backgroundSessionCompletionHandler {
  
                  appDelegate.backgroundSessionCompletionHandler = nil
  
                  DispatchQueue.main.async { completionHandler() }
              }
          }
      }
  
      // --------------------------------------------------------------------------------
  }
  </code></pre>
  
  <p>AppDelegate:</p>
  
  <pre><code>@UIApplicationMain
  class AppDelegate: UIResponder, UIApplicationDelegate {
  
      var window: UIWindow?
      var backgroundSessionCompletionHandler: (() -&gt; Void)?
  
      func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -&gt; Bool {
          // Override point for customization after application launch.
          return true
      }
  
      func applicationWillResignActive(_ application: UIApplication) {
          // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
          // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
      }
  
      func applicationDidEnterBackground(_ application: UIApplication) {
          // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
          // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
      }
  
      func applicationWillEnterForeground(_ application: UIApplication) {
          // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
      }
  
      func applicationDidBecomeActive(_ application: UIApplication) {
          // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
      }
  
      func applicationWillTerminate(_ application: UIApplication) {
          // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
      }
  
      func application(_ application: UIApplication, handleEventsForBackgroundURLSession identifier: String, completionHandler: @escaping () -&gt; Void) {
  
          backgroundSessionCompletionHandler = completionHandler
      }
  }
  </code></pre>
  
  <p>Download:</p>
  
  <pre><code>class Download: NSObject {
  
      var url: String
      var isDownloading = false
      var progress: Float = 0.0
  
      var downloadTask: URLSessionDownloadTask?
      var resumeData: Data?
  
      init(url: String) {
          self.url = url
      }
  }
  </code></pre>
  
  <p>What's wrong with this code:</p>
  
  <ul>
  <li>I'm not sure that the background part is working. I followed this tutorial: <a href="https://www.raywenderlich.com/110458/nsurlsession-tutorial-getting-started" rel="nofollow noreferrer">https://www.raywenderlich.com/110458/nsurlsession-tutorial-getting-started</a>. It says that the app screenshot should be updated if I press home and then double tap home to show the app switcher. Does not seems to work reliably. It is updated when I re-open the app though. Having an iPhone since yesterday, I don't know if this is the normal behavior?</li>
  <li>the 3000 downloads are started in the startDownload method. The maxConcurrentOperationCount of the queue does not seem to be respected: downloads are running concurrently</li>
  <li>the downloadsSession.downloadTask(with: url); call takes 30ms. Multiplied by 3000, it takes 1mn30, that's a big problem :/ . Waiting a few seconds (2-3) would be ok.</li>
  <li>I can't set a delay between two downloads (that's not a big problem. Would be nice though, but if I can't it will be OK)</li>
  </ul>
  
  <p>Ideally, I would run the startDownload method asynchronously, and download the files synchronously in the for loop. But I guess I can't do that in background with iOS?</p>