Hello,
I am trying to download a macOS image with the swift code bellow. I would like to update a NSProgressIndicator component to show download progress.
The closure is run on a background thread so i need to do something to force execution on main thread. This is what i am trying to do with DispatchQueue.main.async but it does not work. I have also tried with DispatchQueue.global().async (and both). I have a runtime error saying i can't update UI from background thread.
I don't understand why DispatchQueue.main.async does not force execution on main thread.
Do you have any idea ?
Thanks
@IBOutlet weak var progression: NSProgressIndicator!
...
func method1()
{
let downloadTask = URLSession.shared.downloadTask(with: restoreImage.url) { localURL, response, error in
...
downloadObserver = downloadTask.progress.observe(\.fractionCompleted, options: [.initial, .new]) { (progress, change) in
DispatchQueue.global().async
{
DispatchQueue.main.async
{
self.progression.doubleValue = (change.newValue! * 100) // Execution error on this line
}
}
...
}