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.
The problem here is: the block in dispatch_async
will be completed immediately, because task
will run on another thread. So, if urls
's length is 100, it will create 100 threads immediately.
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) -> Void in
println("completed")
})
task.resume()
}
}
How can I configure the block in dispatch_async
to wait for the download task to complete? I don't want use dispatch_semaphore
, because it only allow run one download task at the same time.