-
-
Save laevandus/5c87f7db46825575d617244fbe52045a to your computer and use it in GitHub Desktop.
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
@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