Dismissing View Controller not stopping Async Task

前端 未结 2 927
你的背包
你的背包 2021-01-07 13:07

I\'m using the following code to do some complex background operations from a newly launched view controller

 let globalQueue = DispatchQueue.global()
  glob         


        
2条回答
  •  清酒与你
    2021-01-07 13:13

    Create the dispatch work item.

    //create the dispatch work item
    var disptachWorkItem:DispatchWorkItem?
    

    Now put the complex task in dispatch work item.

    dispatchWorkItem = DispatchWorkItem {
    //Some complex job
    }
    

    Execute the Work on default global queue.

    DispatchQueue.global().async(execute: dispatchWorkItem!)
    

    You can find out when the viewController is being dismissed.

    DispatchQueue.global.async {
    dispatchWorkItem?.cancel()
    }
    presentingViewController?.dismissViewController(animated: true, completion: nil)
    

提交回复
热议问题