title: How To Download Multiple Files Sequentially using NSURLSession downloadTask in Swift
body: <p>I have an app that has to download multiple large files. I want it to download each file one by one sequentially instead of concurrently. When it runs concurrently the app gets overloaded and crashes. </p>
  
  <p>So. Im trying to wrap a downloadTaskWithURL inside a NSBlockOperation and then setting the maxConcurrentOperationCount = 1 on the queue. I wrote this code below but it didnt work since both files get downloaded concurrently.</p>
  
  <pre><code>import UIKit
  
  class ViewController: UIViewController, NSURLSessionDelegate, NSURLSessionDownloadDelegate {
  
      override func viewDidLoad() {
          super.viewDidLoad()
          // Do any additional setup after loading the view, typically from a nib.
          processURLs()        
      }
  
      func download(url: NSURL){
          let sessionConfiguration = NSURLSessionConfiguration.defaultSessionConfiguration()
          let session = NSURLSession(configuration: sessionConfiguration, delegate: self, delegateQueue: nil)
          let downloadTask = session.downloadTaskWithURL(url)
          downloadTask.resume()
      }
  
      func processURLs(){
  
          //setup queue and set max conncurrent to 1
          var queue = NSOperationQueue()
          queue.name = "Download queue"
          queue.maxConcurrentOperationCount = 1
  
          let url = NSURL(string: "http://azspeastus.blob.core.windows.net/azurespeed/100MB.bin?sv=2014-02-14&amp;sr=b&amp;sig=%2FZNzdvvzwYO%2BQUbrLBQTalz%2F8zByvrUWD%2BDfLmkpZuQ%3D&amp;se=2015-09-01T01%3A48%3A51Z&amp;sp=r")
          let url2 = NSURL(string: "http://azspwestus.blob.core.windows.net/azurespeed/100MB.bin?sv=2014-02-14&amp;sr=b&amp;sig=ufnzd4x9h1FKmLsODfnbiszXd4EyMDUJgWhj48QfQ9A%3D&amp;se=2015-09-01T01%3A48%3A51Z&amp;sp=r")
  
          let urls = [url, url2]
          for url in urls {
              let operation = NSBlockOperation { () -&gt; Void in
                  println("starting download")
                  self.download(url!)
              }
  
              queue.addOperation(operation)            
          }
      }
      override func didReceiveMemoryWarning() {
          super.didReceiveMemoryWarning()
          // Dispose of any resources that can be recreated.
      }
  
      func URLSession(session: NSURLSession, downloadTask: NSURLSessionDownloadTask, didFinishDownloadingToURL location: NSURL) {
          //code
      }
  
      func URLSession(session: NSURLSession, downloadTask: NSURLSessionDownloadTask, didResumeAtOffset fileOffset: Int64, expectedTotalBytes: Int64) {
          //
      }
  
      func URLSession(session: NSURLSession, downloadTask: NSURLSessionDownloadTask, didWriteData bytesWritten: Int64, totalBytesWritten: Int64, totalBytesExpectedToWrite: Int64) {
          var progress = Double(totalBytesWritten) / Double(totalBytesExpectedToWrite)
          println(progress)
      }
  
  }
  </code></pre>
  
  <p>How can write this properly to achieve my goal of only download one file at a time.</p>