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

528
Visualizações
When using `DispatchQueue.main` as a parameter/variable it's impossible to satisfy the `@MainActor` requirements

When using DispatchQueue.main as a parameter/variable it's impossible to satisfy the @MainActor requirements.

See the following code:

final class MyClass {
    init() {
        DispatchQueue.main.async {
            self.someMainFunction() // legal
        }

        let queue = DispatchQueue.main
        queue.async {
            assert(Thread.isMainThread) // true
            self.someMainFunction() // Call to main actor-isolated instance method 'someMainFunction()' in a synchronous nonisolated context
        }

        queue.async { @MainActor in // Converting function value of type '@MainActor () -> ()' to '@convention(block) () -> Void' loses global actor 'MainActor'
            assert(Thread.isMainThread) // true
            self.someMainFunction()
        }

        // or another example

        doSomethingAsync(queue: .main) { [weak self] in
            assert(Thread.isMainThread) // true
            self?.someMainFunction() // Call to main actor-isolated instance method 'someMainFunction()' in a synchronous nonisolated context
        }
    }

    func doSomethingAsync(queue: DispatchQueue, something: @escaping () -> Void) {
        queue.async {
            something()
        }
    }

    @MainActor
    func someMainFunction() {
    }
}

Any ideas for workarounds? The only thing I've found is using @MainActor(unsafe) on someMainFunction() but I really don't want to do that.

over 4 years ago · Santiago Trujillo
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