Skip to content

Instantly share code, notes, and snippets.

@erdemildiz
Created January 30, 2023 07:38
Show Gist options
  • Save erdemildiz/5d1a133893233e5ab81d3ceec9e0316f to your computer and use it in GitHub Desktop.
Save erdemildiz/5d1a133893233e5ab81d3ceec9e0316f to your computer and use it in GitHub Desktop.
Task Group
// Source: https://www.avanderlee.com/concurrency/task-groups-in-swift/
let images = try await withThrowingTaskGroup(of: UIImage.self, returning: [UIImage].self) { taskGroup in
let photoURLs = try await listPhotoURLs(inGallery: "Amsterdam Holiday")
for photoURL in photoURLs {
taskGroup.addTask { try await downloadPhoto(url: photoURL) }
}
var images = [UIImage]()
/// Note the use of `next()`:
while let downloadImage = try await taskGroup.next() {
images.append(downloadImage)
}
return images
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment