iOS Swift Dispatch

1.延迟1.5秒执行:
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.异步执行回到主线程
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()
             }
    }

你可能感兴趣的:(iOS Swift Dispatch)