I'm trying to use the new structured concurrency features of Swift 5.5 to update some code in a larger App that has been written using GCD. One routine is loading parts of a dataset in a parallel loop and I would like to convert this code using withThrowingTaskGroup while keeping a progress counter. The code compiles, runs, but does not update the progress in a way I expected:
var progressCounter = 0
let count = 500
try await withThrowingTaskGroup(of: Void.self) { group in
for idx in 0..<count {
_ = group.addTaskUnlessCancelled {
// some IO
}
}
for try await _ in group {
progressCounter += 1
}
}
The for try await
loop is executed after all 500 task have been run and finished (iOS 15 Beta 5). I was under the impression, the for try await
loop would be run in parallel to the tasks, collecting results as they come in and thereby updating the progress counter. Clearly I'm missing something or is this expected? Is withThrowingTaskGroup not the right tool for this?
Any insight would be appreciated.