简体   繁体   中英

How to go back to DispatchQueue.main from URLSession.shared.dataTask (macOS framework)

I'm building a macOS framework and at some point, I need to make a request to some API

When I got the response I want to update the UI. I'm using URLSession.shared.dataTask to make the call and as I know the call is made in the background thread

For some reason when I try to go back to the main thread nothing happens

I'm using a virtual machine to run my framework

Any help? Thanks

Here how I doing the request:

URLSession.shared.dataTask(with: request) { data, response, error in
  if error != nil {
    DispatchQueue.main.async {
      //Display error message on the UI
      //This never happens
      //Never go back to the main thread
      //Framework stop working
    }
  }
}.resume()

Are you sure that your task is called?

let dataTask = URLSession.shared.dataTask(with: request, completionHandler: { (data, response, error) -> Void in

....

    DispatchQueue.main.async {
        completion(nil, nil)
    }
}
dataTask.resume() // You should add this. 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM