79145546

Date: 2024-10-31 16:32:27
Score: 1.5
Natty:
Report link

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:

  1. all async staff is sync. If you want to run it async use Task. ( but even u cant use it without task )
  2. Task = DispatchQueue.current.async - BUT on EVERY AWAIT system COULD change Thread.

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

  1. 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

Reasons:
  • Long answer (-1):
  • Has code block (-0.5):
  • Contains question mark (0.5):
  • User mentioned (1): @MainActor
  • User mentioned (0): @Published
  • Self-answer (0.5):
  • Low reputation (1):
Posted by: Dragonboh