【发布时间】:2019-03-27 23:32:18
【问题描述】:
我用 DispatchQueue 做了简单的测试:
DispatchQueue.global(qos: .background).sync {
if Thread.isMainThread {
print("Main thread")
}
}
打印出来了:
Main thread
为什么这段代码在主线程上执行?它应该在后台线程上执行(它被添加到后台队列中),对吧?
【问题讨论】:
-
来自
sync函数的文档:“作为一种优化,这个函数在可能的情况下调用当前线程上的块。” -
不要混淆队列和线程。使用队列,让他们为您担心线程!
标签: ios swift concurrency dispatch-queue