func dispatch_async(_ queue: dispatch_queue_t!, _ block: dispatch_block_t!)
dispatch_queue_t为线程队列,异步的方式将block要执行的代码添加到_queue队列中,等待执行。
1.后台执行
dispatch_async(dispatch_get_global_queue(0, 0), ^{
// something
});
这里func dispatch_get_global_queue(_ identifier: Int, _ flags: UInt) -> dispatch_queue_t!
identifier是提供给要执行代码到队列里的服务质量,决定优先级;flags留作以后用,一般置0.
2.主程序执行dispatch_async(dispatch_get_main_queue(), ^{
// something
});3.执行一次
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
// code to be executed once
});
本文介绍了如何使用Grand Central Dispatch (GCD)进行异步任务处理。包括如何将任务添加到全局队列或主线程队列中执行,以及如何确保某个代码块仅执行一次。
1936

被折叠的 条评论
为什么被折叠?



