This is really a more idiomatic way:
struct BookProvider: PreviewModifier {
static func makeSharedContext() async -> [Book] {
let url = Bundle.main.url(forResource: "Curs confirmare RO", withExtension: "pdf")!
return await withTaskGroup { tg in
for _ in 0..<10 {
tg.addTask {
return await createBook(for: url, of: CGSize(width: 200, height: 200), scale: 4.0)
}
}
var books: [Book] = []
for await result in tg {
books.append(result)
}
return books
}
}
func body(content: Content, context: [Book]) -> some View {
ContentView(books: context)
}
}
#Preview(traits: .modifier(BookProvider())) {
ContentView()
}
Topic:
UI Frameworks
SubTopic:
SwiftUI
Tags: