Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

539
Vistas
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 la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda