【发布时间】:2022-01-23 13:30:48
【问题描述】:
@MainActor 只是 DispatchQueue.main.async 的语法糖还是有其他用途?
【问题讨论】:
@MainActor 只是 DispatchQueue.main.async 的语法糖还是有其他用途?
【问题讨论】:
如果我们查看UILabel 和UIViewController 的声明,我们可以看到它们都被新的@MainActor 属性注释:
@MainActor class UILabel: UIView
@MainActor class UIViewController: UIResponder
这意味着,当使用 Swift 的新并发系统时,这些类(以及它们的任何子类)上的所有属性和方法都将自动在主队列中设置、调用和访问。所有这些调用都将自动通过系统提供的MainActor 进行路由,该MainActor 始终在主线程上执行所有工作——完全消除了我们手动调用DispatchQueue.main.async 的需要。
来源:https://www.swiftbysundell.com/articles/the-main-actor-attribute/
【讨论】: