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

4K
Vistas
How to capture local variable inside an async closure in Swift?

I have the following code in Swift 5.5 and iOS 15

func getReviewIds() {
    
    var reviewIds: [Int] = []
    
    Task {
        let ids = await getReviewIdsFromGoogle()
        reviewIds.append(contentsOf: ids)
    }
    
    print("outside")
}

func getReviewIdsFromGoogle() async -> [Int] {
    await withUnsafeContinuation { continuation in
        DispatchQueue.main.asyncAfter(deadline: .now() + 2.0) {
            continuation.resume(returning: [1,2,3])
        }
    }
}

I get an error in getReviewIds function on the following line:

 reviewIds.append(contentsOf: ids)

Mutation of captured var 'reviewIds' in concurrently-executing code

I know that I can make the getReviewId an async function instead of using the async closure, but how can I solve this using the closure.

over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

You can't pass data back to the original synchronous context once you have started an async context, as this would require the original context to "block" while it waits for the asynchronous results. Swift does not allow blocking in its concurrency model as this could lead to a thread deadlock.

You will have to just call another function with the results from the Task context to process the returned values. It's up to you if this process is another async function or not, depending on what you need to do.

func getReviewIDs() {
    Task {
        let result = await getReviewIdsFromGoogle()
        process(ids: result)
    }
}

func process(ids: [Int]) {
    print("now process ids: \(ids)")
}

func getReviewIdsFromGoogle() async -> [Int] {
    await withUnsafeContinuation { continuation in
        DispatchQueue.main.asyncAfter(deadline: .now() + 2.0) {
            continuation.resume(returning: [1,2,3])
        }
    }
}
over 4 years ago · Santiago Trujillo Denunciar

0

To prevent data races you must use synchronized access to variables from concurrent operations and the compiler doesn't allow you to change your array directly. To avoid the issue you can implement isolated access to your data with an actor instance e.g.:

actor Store {
    var reviewIds: [Int] = []
    func append(ids: [Int]) {
        reviewIds.append(contentsOf: ids)
    }
}

func getReviewIds() {
    
    let store = Store()
    
    Task {
        let ids = await getReviewIdsFromGoogle()
        await store.append(ids: ids)
        print(await store.reviewIds)
    }
}
over 4 years ago · Santiago Trujillo Denunciar
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