【问题标题】:Swift 中的@MainActor 是什么? [关闭]
【发布时间】:2022-01-23 13:30:48
【问题描述】:

@MainActor 只是 DispatchQueue.main.async 的语法糖还是有其他用途?

【问题讨论】:

标签: ios swift swift5.5


【解决方案1】:

如果我们查看UILabelUIViewController 的声明,我们可以看到它们都被新的@MainActor 属性注释:

@MainActor class UILabel: UIView
@MainActor class UIViewController: UIResponder

这意味着,当使用 Swift 的新并发系统时,这些类(以及它们的任何子类)上的所有属性和方法都将自动在主队列中设置、调用和访问。所有这些调用都将自动通过系统提供的MainActor 进行路由,该MainActor 始终在主线程上执行所有工作——完全消除了我们手动调用DispatchQueue.main.async 的需要。

来源:https://www.swiftbysundell.com/articles/the-main-actor-attribute/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-02-11
    • 2015-03-17
    • 1970-01-01
    • 1970-01-01
    • 2016-12-21
    • 2018-02-23
    • 1970-01-01
    • 2015-05-26
    相关资源
    最近更新 更多