2
import Foundation
import Dispatch

DispatchQueue.main.asyncAfter(deadline: .now() + 2.0) {
   print("fffffff")
}

run by swift but program ends without output:

enter image description here

Rob
  • 415,655
  • 72
  • 787
  • 1,044
eczn
  • 1,459
  • 2
  • 10
  • 15
  • 1
    The app finishes before the async closure gets a chance to finish. You could theoretically spin on a runloop to keep it alive. Or you can `await` an `async` `Task` in Swift concurrency, eliminating GCD. – Rob Jul 16 '23 at 15:41
  • 1
    E.g., in Swift concurrency it might just be https://gist.github.com/robertmryan/0e03d30027b3735a0dcf4f040bd8c262. Or, in a example that Sweeper shared, https://stackoverflow.com/a/76699185/1271826. Bottom line, if using old completion-handler patterns, you need kludgy workarounds to keep the app alive, whereas it follows quite naturally in Swift concurrency’s `async`-`await`. Now, I’m gathering from your source file name, `setTimeout.swift` (!) that there is a more complicated timeout scenario that you are contemplating, but those are also done quite naturally with Swift concurrency. – Rob Jul 16 '23 at 18:32

0 Answers0