如何让异步方法同步执行
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let sem = DispatchSemaphore(value: 0)
DispatchQueue.global().async {
print("test async")
sem.signal()
}
sem.wait(timeout: DispatchTime.distantFuture)
print("test main")
}
// 输出
test async
test main
网友评论