Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is @MainActor in Swift? [closed]

Tags:

ios

swift

swift5

Is @MainActor just a syntactic sugar for DispatchQueue.main.async or else is there any other use to it?

like image 326
Shreeram Bhat Avatar asked Sep 18 '25 13:09

Shreeram Bhat


1 Answers

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/

like image 52
Dima Rostopira Avatar answered Sep 20 '25 04:09

Dima Rostopira