ios - Using GCD in Swift CLI -
so i'm trying use gcd in cli. test out wrote code this:
import foundation var = 0 print("a: ",i) dispatch_async(dispatch_get_global_queue(qos_class_user_initiated, 0)) { n in 1..<10{ i++ } print("c: ",i) dispatch_async(dispatch_get_main_queue()){ print("d: ",i) } } print("b: ",i) sleep(5) print("e: ",i)
the output of is: a: 0 b: 0 c: 9 e: 9
with last line printing few seconds later. i'm trying find out happened @ d? nothing put in block seems execute. works fine when use in ios, not in cli.
the cli lacks persistence of app. came end (terminated) before d
had chance printed.
as @user3441734 rightly points out, can work around in cli calling dispatch_main()
last thing before exit. call forces dip main queue right now , pull out main-queued block , execute it, before exiting.
Comments
Post a Comment