1.延遲1.5秒執(zhí)行:
Object-C:
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(1.5 * Double(NSEC_PER_SEC))), dispatch_get_main_queue()) {
print("test")
}
Swift
//延遲1.5秒跳轉
DispatchQueue.main.asyncAfter(deadline: .now() + 1.5) {
self.presentToRootVc()
}
2.異步執(zhí)行回到主線程
Object-C
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) {
let image = self.loadImage()
// 回到主線程更新UI
dispatch_async(dispatch_get_main_queue()) {
self.imageView.image = image
}
}
Swift
//異步回到主線程開啟相機
DispatchQueue.global(qos: .userInitiated).async {
//處理耗時操作的代碼塊...
DispatchQueue.main.async {
//操作完成,調用主線程來刷新界面
self.cameraTakePhoto()
}
}