I'm using the following code to do some complex background operations from a newly launched view controller
let globalQueue = DispatchQueue.global()
globalQueue.async {
//complex job
}
When the user clicks on the cancel button, I call
self.dismissViewController(self)
But it seems this does not stop the background task.
How can I solve this problem?
There is no API that can cancel running thread. Instead, can add cancellation flag to check in handling response.
Create the dispatch work item.
Now put the complex task in dispatch work item.
Execute the Work on default global queue.
You can find out when the viewController is being dismissed.