title: Swift: How to download synchronously?
body: <p>I am using following code, to download some files:</p>
  
  <pre><code>    let url = NSURL(string:"https://www.example.org/")!
      let task = NSURLSession.sharedSession().dataTaskWithURL(url) { (data, response, error) in
          if error != nil {
              NSLog("%@", "Error: \(error)");
              return
          }
  
          NSLog("Loaded %i bytes", data!.length)
      }
      task.resume()
  </code></pre>
  
  <p>I want to process some of the files and quit my application after downloading. Therefore I need to know, when the download process is finished. The best would be, if there is a way to do this synchronously (no problem, if UI is blocked - it is just a spash screen with a progressbar). But as far as I understood this topic after some research, this is not possible in Swift any more...</p>
  
  <p>I did wrap this code in a function, therefore I can't just add some code after the NSLog statement. What I need to know is: When did the last file finish downloading? How can I retrive this information?</p>
  
  <p><strong>EDIT</strong>: This code did work for me (but be aware, its deprecated!):</p>
  
  <pre><code>    // download file synchonously ////////////////////////////////////////////////////
      func downloadSync(fromURL: String, toPath: String) {
          let request = NSURLRequest(URL: NSURL(string: fromURL)!)
          var response: NSURLResponse?
          do {
              let data = try NSURLConnection.sendSynchronousRequest(request, returningResponse: &amp;response)
              data.writeToFile(toPath, atomically: true)
          } catch {
              print("Error while trying to download following file: " + fromURL)
          }
      }
  </code></pre>