我必须在主线程上执行类似 NSProgressIndicator.stopAnimation
的操作。但是我想在分配给后台线程的 workItem 结束时执行此操作。
NSProgressIndicator.startAnimation(self)
let workItem = DispatchWorkItem {
//
// Some tasks
//
// This doesn't work, how do I call it on main thread?
NSProgressIndicator.stopAnimation(self)
}
DispatchQueue.global(qos: .background).async(execute: workItem)
最佳答案
使用 DispatchQueue.main.async
就像在主队列上运行任何代码一样。
let workItem = DispatchWorkItem {
//
// Some tasks
//
DispatchQueue.main.async {
NSProgressIndicator.stopAnimation(self)
}
}
关于swift - 当 workItem 在后台线程上完成时,如何通知主线程?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48493039/