Post marked as solved
Post marked as solved with 1 replies, 813 views
I'm having trouble understanding the use case for discardingTaskGroup. In my app, I want to submit 10 concurrent image upload requests; usually, I'd just fire off 10 unstructured Task {} instances (I'm assuming this is fine)
for image in images {
Task {
do {
try await uploadImage(item: item,
image: image)
} catch {
// Handle any errors
}
}
}
But then I thought I'd actually like to have a max of ~3 uploads concurrently, where I would prioritize the images that appear to the user earlier first. I know using group.next() in a taskGroup we can await on previous results and add tasks as required.
But my task does not return data, rather it performs an action. So, it seems like the new discardingTaskGroup could be a useful API.
Task {
do {
try await withThrowingDiscardingTaskGroup { group in
for image in images {
group.addTask {
try await uploadImage(item: item,
image: image)
}
}
}
} catch {
// Handle any errors
}
}
How can I convert this discarding task group code to only include a max of n tasks running concurrently? And is this even a reasonable use of the new API to begin with?
Best,
T