Daniel Marino
3 November 2024
Fixing the Custom UIView Initialization Main Actor Isolation Error in Swift 6

Developers may see an unexpected main actor isolation issue in their UIView subclasses when updating to Swift 6, particularly when initializing with awakeFromNib(). Calling main actor-isolated methods, such as addContentView(), in a synchronous, nonisolated context frequently results in this issue. The new concurrency restrictions in Swift 6 are intended to increase performance and safety, but they also call for changes to long-standing procedures. This article explains how to fix these problems and enable safe and efficient UI setup on the main thread by using utilities like MainActor.assumeIsolated and Task.