what actor does a task run on
Generated on 8/3/2024
1 search
A task runs on the actor it is associated with. In Swift, actors are reference types that encapsulate shared mutable state and automatically protect their state by serializing accesses. Only a single task is allowed to execute at a time on an actor, and calls to actor methods from outside the context of the actor are asynchronous.
For example, if you have a method that is isolated to the main actor, it means that the method will run on the main actor. This can be explicitly stated using mainactor assume isolated
, which tells Swift that the code is already running on the main actor and does not start a new task to async onto the main actor.
Here are some relevant sessions and timestamps that discuss actors and tasks:
- A Swift Tour: Explore Swift’s features and design - Discusses how actors serialize accesses and ensure that only a single task executes at a time on an actor.
- Migrate your app to Swift 6 - Explains the use of
mainactor assume isolated
to indicate that code is running on the main actor without starting a new task. - Migrate your app to Swift 6 - Describes how methods can be isolated to the main actor and how to handle asynchronous calls to actor-isolated state.
Relevant Sessions
- Migrate your app to Swift 6
- A Swift Tour: Explore Swift’s features and design
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
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.
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.
Train your machine learning and AI models on Apple GPUs
Learn how to train your models on Apple Silicon with Metal for PyTorch, JAX and TensorFlow. Take advantage of new attention operations and quantization support for improved transformer model performance on your devices.