After the serialQueue has finished downloading task, I want to return all the downloaded object. I want to track when the serial Queue has finished its tasks. So, is there any completion handler in Serial GCD? Or, do I have to use NSOperationQueue for this purpose?
func serialGCD(links: [String]) -> [String] {
let data: [String] = []
let serialQueue = DispatchQueue(label: "com.self.serialGCD")
links.forEach { (x) in
serialQueue.async {
//data task
//data.append(downloadedData)
}
}
return data
}
You can use DispatchGroup here to achieve completion like behaviour. You can use DispatchGroup to submit multiple tasks and track when they all complete, even though they might run on different queues.
func serialGCD(links: [String]) -> [String] {
let data: [String] = []
let serialQueue = DispatchQueue(label: "com.self.serialGCD")
let group = DispatchGroup()
links.forEach { (x) in
group.enter()
serialQueue.async {
//data task
//data.append(downloadedData)
group.leave()
}
}
group.notify(queue: .main) {
//Completion block
}
return data
}