Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

149
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda