I am using a task group to wrap repeated calls to a long running async method. Since I may have many calls to this method that need to happen the hope is that they would run in parallel. At the end of the day I have some regular synchronous code that needs to block until all of these asyncs are called.
What's weird is unlike other examples of task groups I have seen I actually do not need the values from the async throws method. They do actions and write them to disk.
Is there not a way to clean up this code?
let swiftAsyncSemaphor = DispatchSemaphore(value: 0)
let taskGroupTask = Task {
await withThrowingTaskGroup(of: ASRJob.self) { group in
for path in filePathsToWorkOn {
group.addTask {
return try await doHardWork(atPath: path)
}
}
do {
for try await _ in group {
// For now we do not store this information
}
} catch {
// For now we do not store the error
}
}
swiftAsyncSemaphor.signal()
}
swiftAsyncSemaphor.wait()
Most examples I see use a map/combine function at the end on the group but I don't need the data so how can I just await for all of them to finish?
Context on where this code will run: This code is run within the main() block of a Synchronous Operation (NSOperation) that goes within an OperationQueue. So the block calling wait is not in swift async code. The goal is to block the Operations completion until this swift async work is done knowing that it may very well run for a long time.