I'm working on a GUI app, so inside a Task
I want to update the UI, which is only possible from the main thread.
There is the old GCD way of doing it, with libdispatch's DispatchQueue
, but I was wondering if there's a more modern way of doing it with a Task
Task.detached(priority: .background) {
let result = await doSomeHeavyAsyncTask()
// this now needs to be done on the main thread
myWidget.update(result)
}
I think it would have been nice if we'd have a continuation like:
Task.detached(priority: .background) {
let result = await doSomeHeavyAsyncTask()
// this now needs to be done on the main thread
return result
}, continuationOnMain: { result in
//this runs on main thread
myWidget.update(result)
}