skip to Main Content
@MainActor
class A {}

class VC: UIViewController {
  let foo: A

  init() {
    self.foo = A() // Error: Call to main actor-isolated initializer 'init()' in a synchronous nonisolated context
    super.init(nibName: nil, bundle: nil)
  }

  @MainActor
  init() {
    self.foo = A() // Compiles
    super.init(nibName: nil, bundle: nil)
  }

  init(_ void: Void = ()) { 
    self.foo = A() // Compiles
    super.init(nibName: nil, bundle: nil)
  }
}

I get that A() should be called on a main-actor initializer, but why the first initializer doesn’t inherit the @MainActor from UIViewController? and why the Void parameter gets rid of the error (even with -warn-concurrency)?

Note: if I annotate VC with @MainActor, I get the same error for the super.init in the first initializer, and the third initializer still works.

2

Answers


  1. @MainActor limits all methods and attributes from being assessable from the main thread in the class A.

    let foo: A is not the class A it an instance that stores a reference to the class A, you have done nothing to make setting foo thread safe. One of the init of VC is setup to only be executed in the main thread only, where foo is set, but another can be called by any thread and set foo as well.

    Edit: Some more info I thought you might find useful
    If your sole goal is thread safety, then using MainActor is not a good method, it means only the thread associated with GUI interaction can access your class, which could block some actual code that has to run in the main thread from executing, for just thread safety you are probable better of using actors, they work like classes, but only one thread can be in them at one time, they can be any thread, and different methods can be access by different threads a different times, but only one will be in but actor at a time.

    Login or Signup to reply.
  2. You can also solve the problem by adding an initializer and marking it nonisolated.

    @MainActor
    class A {
        nonisolated init() {}
        func load() async -> String {
            return "Async!"
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search