Swift 后台执行代码封装库 Async。
Swift 中写GDC(Grand Central Dispatch)类似这样的:
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), {
print("This is run on the background queue")
dispatch_async(dispatch_get_main_queue(), {
print("This is run on the main queue, after the previous block")
})
}) 使用
Async 的要简洁的多:
Async.background {
print("This is run on the background queue")
}.main {
print("This is run on the main queue, after the previous block")
}
Async支持的方法:
Async.main {}
Async.userInteractive {}
Async.userInitiated {}
Async.utility {}
Async.background {} 支持多个代码块:
Async.userInitiated {
// 1
}.main {
// 2
}.background {
// 3
}.main {
// 4
}