What’s the best way to use concurrency?

Asked on 06/12/2025

1 search

To effectively use concurrency in Swift, it's important to understand the concurrency model and how to apply it to your app. Here are some key points from the WWDC sessions on Swift concurrency:

  1. Start with Single-Threaded Code: Begin by running all your code on the main thread. This is where your app receives UI-related events and can update the UI in response. Single-threaded code is easier to write and maintain (Embracing Swift concurrency).

  2. Introduce Asynchronous Tasks: As your app grows, you may need to perform tasks that take a long time, such as network requests. Introduce asynchronous tasks to handle these operations without blocking the main thread (Embracing Swift concurrency).

  3. Use Background Threads for Expensive Computation: Move expensive computations to background threads to keep the app responsive. Swift's concurrency model allows you to offload work to the background safely (Embracing Swift concurrency).

  4. Leverage Actors and Nonisolated Code: Use actors to manage state that needs to be accessed concurrently. Nonisolated code can be used when you want to separate certain operations from the main actor (Embracing Swift concurrency).

  5. Structured Concurrency: Use structured concurrency to manage the lifecycle of concurrent tasks. This helps in organizing and controlling asynchronous code (Code-along: Elevate an app with Swift concurrency).

  6. Concurrency in SwiftUI: In SwiftUI, separate your UI code from non-UI code. Use state as a bridge to initiate async tasks and update the UI synchronously when tasks complete (Explore concurrency in SwiftUI).

By following these guidelines, you can effectively use concurrency to improve the performance and responsiveness of your app while avoiding common pitfalls like data races. For a deeper dive, consider watching the full sessions on Embracing Swift concurrency and Explore concurrency in SwiftUI.