Tasks in Swift Concurrency that inherit MainActor isolation start executing on the main thread even when they don't need to, causing unnecessary suspension points and performance degradation. The post demonstrates how a Task created inside a @MainActor class immediately occupies the main thread before suspending to call an async method, and how running many such tasks compounds the problem. Using Xcode Instruments' Swift Concurrency template, these suspension patterns become visible. The fix is to annotate the task closure with `@concurrent` so it starts off the main thread, then explicitly hop back with `MainActor.run` only when needed for UI updates.

6m read timeFrom avanderlee.com
Post cover image
Table of contents
A busy Main-ThreadAn unexpected suspension pointVisualizing the problem using Xcode InstrumentsSolving the unexpected suspension point

Sort: