#88: @MainActor in Swift

@MainActor marks declarations that should run on the main actor, which is exactly what UI-related code often needs.

@MainActor
final class ProfileViewModel {
    var title = ""

    func updateTitle(_ value: String) {
        title = value
    }
}

This is more expressive than manually sprinkling DispatchQueue.main.async all over the place.

It also lets the compiler participate in keeping actor-isolated code honest.


I'm also on Twitter and GitHub.

Subscribe via RSS.