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 getReviewIdsFromGoogle
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 getReviewIdsFromGoogle
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)
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With