Created
January 30, 2023 07:38
-
-
Save erdemildiz/5d1a133893233e5ab81d3ceec9e0316f to your computer and use it in GitHub Desktop.
Task Group
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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