Cancelling Dispatch Blocks
- You can only cancel DispatchWorkItem before it reaches the head of a queue and starts executing.
let workItem = DispatchWorkItem(flags: .inheritQoS) {
let url = URL(string: "")!
let task = URLSession.shared.dataTask(with: url, completionHandler: { (data, response, error) in
// Update UI
})
task.resume()
}
DispatchQueue.global().async(execute: workItem)
workItem.cancel()