Skip to content

Instantly share code, notes, and snippets.

@laevandus
Created August 7, 2022 03:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save laevandus/5c87f7db46825575d617244fbe52045a to your computer and use it in GitHub Desktop.
Save laevandus/5c87f7db46825575d617244fbe52045a to your computer and use it in GitHub Desktop.
@MainActor final class ViewModel: ObservableObject {
let imageNames: [String]
init(imageNames: [String]) {
self.imageNames = imageNames
}
func load() {
Task {
let store = ImageStore()
let images = try await withThrowingTaskGroup(of: UIImage.self, body: { group in
imageNames.forEach { imageName in
group.addTask {
try await store.loadImage(named: imageName)
}
}
return try await group.reduce(into: [UIImage](), { $0.append($1) })
})
self.images = images
}
}
@Published var images = [UIImage]()
}
struct ImageStore {
func loadImage(named name: String) async throws -> UIImage {
return …
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment