Is @MainActor
just a syntactic sugar for DispatchQueue.main.async
or else is there any other use to it?
If we take a look at the declarations for both UILabel
and UIViewController
, we can see that they’ve both been annotated with the new @MainActor
attribute:
@MainActor class UILabel: UIView
@MainActor class UIViewController: UIResponder
What that means is that, when using Swift’s new concurrency system, all properties and methods on those classes (and any of their subclasses) will automatically be set, called, and accessed on the main queue. All those calls will automatically be routed through the system-provided MainActor
, which always performs all of its work on the main thread — completely eliminating the need for us to manually call DispatchQueue.main.async
.
Source: https://www.swiftbysundell.com/articles/the-main-actor-attribute/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With