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.


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)
    }
}

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])
        }
    }
}