Too much text about Actors and isolation. For someone who don;t understand how task is worked, and someone who still think Task.detached is bad and have very narrow usecases.
So what I understand after some experiments and learning. All is simple. Apple did ( like every time ) old staff from past years in new cover. so:
What does it mean? If you have something like this:
class A {
func first() async {
for i in 0...1000 {
print("🤍 \(i)")
}
}
} class ViewController: UIViewController {
let a = A()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
Task {
await a.first()
}
second()
}
func second() {
Thread.sleep(forTimeInterval: 5)
for i in 0...1000 {
print("♥️ \(i)")
}
}
}
Second() will always print first, but First() could print on other Thread then main
If you need DispatchQueue.global.async you use Task.detached
await its like sync on GCD this code :
Task { await a.first() }
is something like
DispatchQueue.current.async {
DispatchQueue.global().sync {
a.first
}
}
What does it mean? - If you have @MainActor functions in your class for updating @Published properties, and you for example what update a Progress of some background work ( for example AsyncSequence ) you must use Task.detached ( what ever Apple say )
Apple problem is that they every time think that all apps in Store is 3 screen MVP whit one button and one endpoint