Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

151
Views
How to properly get rid of this concurrency compile error?
@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.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

@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.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!