I’m trying to replace DispatchGroup
with a TaskGroup
because I can cancel a TaskGroup
… however I can’t store a reference to a TaskGroup
..
The methods withTaskGroup(...)
and withDiscardingTaskGroup(...)
seem to be built around the assumption that you’ll always have all of your child tasks you’ll ever have in the task group exactly ready on the next line. However what if you have a bunch of code without references to one another and you want to throw your tasks into the common group? Like if you want to track them, queue them, cancel them later, etc? It seems you can’t… at least that is what this question is for..
Example code:
class SHACalculationGroup {
// 'DiscardingTaskGroup' initializer is inaccessible due to 'internal' protection level
internal let taskGroup: TaskGroup = DiscardingTaskGroup()
let lock = AsyncSemaphore(value: 8)
private func calculateShaHash(forUrl url: URL) async {
await lock.wait()
do {
try let sha256 = url.sha256().hexStr
log.debug("sha256 is (sha256 ?? "EMPTY")")
} catch {
log.error("Could not get sha256 of the file. (error)")
}
lock.signal()
}
func kickoffShaCalculation(forUrl url: URL) {
taskGroup.addTask(operation:
Task.detached(priority: .utility) {
await calculateShaHash(forUrl: url)
}
)
}
}
I get the error on the init for DiscardingTaskGroup
: 'DiscardingTaskGroup' initializer is inaccessible due to 'internal' protection level
Seriously what the heck.. how are we supposed to centralize random common operations from around our application without being able to store a reference to this thing? I’d like to know what in the heck they were thinking if you might be able to comment on that as well.
Thanks for any help!