I'm trying out Swift Concurrency on a project, and I wanted to isolate all the logic in a singleton to the main actor.
I tried annotating the singleton class with @MainActor and leaving the rest as is (I'm using a common Singleton pattern in Swift, which relies on atomic global variable initialization):
private var manager = FooManager()
@MainActor
final class FooManager {
class var shared: FooManager { return manager }
}
This fails with a compiler error on the first line:
Call to main actor-isolated initializer 'init()' in a synchronous
nonisolated context
That error makes perfect sense, but it does leave me wondering if I can do what I want, i.e. use the Swift singleton pattern but keep the singleton isolated to the main actor.
That I cannot answer. As far as I know static properties are all lazy, but if I recall correctly so are global properties, that's as far my knowledge goes here.