difference between `MainActor.run` and `Task {@MainActor in...}`

Generated on 5/16/2025

1 search

Tailscan

This post is brought to you by Tailscan, the best devtool for Tailwind CSS.

The difference between MainActor.run and Task {@MainActor in...} lies in how they handle execution on the main actor in Swift's concurrency model.

  1. MainActor.run: This is a synchronous way to execute code on the main actor. It ensures that the code block is executed on the main actor, but it does not create a new asynchronous task. This is useful when you need to perform operations on the main actor without introducing additional asynchronous behavior.

  2. Task {@MainActor in...}: This creates a new asynchronous task that is executed on the main actor. It is useful when you want to perform asynchronous operations that need to be isolated to the main actor. This approach is more flexible as it allows for asynchronous code execution, which can be beneficial for tasks that involve waiting for results or performing non-blocking operations.

In summary, use MainActor.run for synchronous operations that need to be on the main actor, and Task {@MainActor in...} for asynchronous operations that should be isolated to the main actor.

Go further with Swift Testing

Go further with Swift Testing

Learn how to write a sweet set of (test) suites using Swift Testing’s baked-in features. Discover how to take the building blocks further and use them to help expand tests to cover more scenarios, organize your tests across different suites, and optimize your tests to run in parallel.

Explore Swift performance

Explore Swift performance

Discover how Swift balances abstraction and performance. Learn what elements of performance to consider and how the Swift optimizer affects them. Explore the different features of Swift and how they’re implemented to further understand the tradeoffs available that can impact performance.

Consume noncopyable types in Swift

Consume noncopyable types in Swift

Get started with noncopyable types in Swift. Discover what copying means in Swift, when you might want to use a noncopyable type, and how value ownership lets you state your intentions clearly.

Meet Swift Testing

Meet Swift Testing

Introducing Swift Testing: a new package for testing your code using Swift. Explore the building blocks of its powerful new API, discover how it can be applied in common testing workflows, and learn how it relates to XCTest and open source Swift.

Migrate your app to Swift 6

Migrate your app to Swift 6

Experience Swift 6 migration in action as we update an existing sample app. Learn how to migrate incrementally, module by module, and how the compiler helps you identify code that’s at risk of data races. Discover different techniques for ensuring clear isolation boundaries and eliminating concurrent access to shared mutable state.

A Swift Tour: Explore Swift’s features and design

A Swift Tour: Explore Swift’s features and design

Learn the essential features and design philosophy of the Swift programming language. We’ll explore how to model data, handle errors, use protocols, write concurrent code, and more while building up a Swift package that has a library, an HTTP server, and a command line client. Whether you’re just beginning your Swift journey or have been with us from the start, this talk will help you get the most out of the language.