In Swift, withTaskGroup is used to get task completed data as:
let test1 = await withTaskGroup { grp in
grp.addTask { "Task 1 data" }
grp.addTask { "Task 2 data" }
}
for await result in grp {
print(result) // got task completed data
}
How we can get data from task completed bywithDiscardingTaskGroup?
let test1 = await withDiscardingTaskGroup {grp in
grp.addTask { "Task 1 data" }
grp.addTask { "Task 2 data" }
}
The whole point of
DiscardingTaskGroupis to discard the results of child tasks. If you want results from child tasks, use a regular task group.This is useful if the child tasks produce side effects, instead of returning values. The documentation says:
A discarding task group is kind of similar to a bunch of
async lets of typeVoid:Of course, the main difference is that a task group can have a dynamic number of child tasks.
Side note: although the child tasks' results are discarded, the task group can still produce its own result, e.g.
getSomeIntegerAsynchronouslywill not wait for the child tasks to complete, and will run in parallel with the child tasks.withDiscardingTaskGrouphowever, will wait for all the child tasks, and alsogetSomeIntegerAsynchronously, to complete, before returning.