Here’s my viewDidLoad
method :
func viewDidLoad() {
// Stuff here
Task { @MainActor in
doSomeWorkOnMainThread1()
doSomeWorkOnMainThread2()
await doSomeBackgroundWork()
doSomeWorkOnMainThread3()
doSomeWorkOnMainThread4()
}
}
And here’s my method that should execute work on a background thread :
func doSomeBackgroundWork() async {
// can add some code here
// long image processing task
assert(!Thread.isMainThread)
// can add some code here
}
Is it possible to execute doSomeBackgroundWork
on a background thread (without using GCD), and wait for it before going back to the main thread ?
2
Answers
This solution is working, but it's not perfect I guess :
You are looking for Task.sleep. Note that it is in nanoseconds which is Really Weird, so I have written an extension to switch to seconds; to get it, you must use a Double, not an Int:
The way to guarantee that a piece of work will be done on a background thread is to give that work to an Actor. That is what an Actor is for (in part).
So for example if an Actor has a method
doSleep
that callsTask.sleep
, then if you instantiate that actor and call that method from your Task, it will sleep on a background thread.