title: Waiting for multiple asynchronous download tasks
body: <p>I want to download some files, for example 100 files, at the same time. So I decided to add my download threads to a dispatch queue, and GCD will adjust how many threads will run at the same time.</p>
  
  <p>The problem here is: the block in <code>dispatch_async</code> will be completed immediately, because <code>task</code> will run on another thread. So, if <code>urls</code>'s length is 100, it will create 100 threads immediately.</p>
  
  <pre><code>var queueDownloadTask = dispatch_queue_create("downloadQueue", nil)
  
  for url in urls {
      dispatch_async(queueDownloadTask) {
  
          let config = NSURLSessionConfiguration.defaultSessionConfiguration()
          let fileTransferSession = NSURLSession(configuration: config)
  
          let task = fileTransferSession.downloadTaskWithURL(url, completionHandler: { (responseUrl, response, error) -&gt; Void in
              println("completed")
          })
          task.resume()
      }
  }
  </code></pre>
  
  <p><strong>How can I configure the block in <code>dispatch_async</code> to wait for the download task to complete?</strong> I don't want use <code>dispatch_semaphore</code>, because it only allow run one download task at the same time.</p>