RequestView is a view that asynchronously loads data from the web.
RequestView is powered by a Request. It handles loading the data, and you can focus on building your app.
It takes a Request, a placeholder and any content you want rendered.
Important: You must handle when
datais nil in your content, as theRequestViewrenders the content and the placeholder at the same time, so it will be rendered at least once when data is nil (and possibly again if the request fails).
RequestView(Request {
Url("https://api.example.com")
}) { data in
<<Content>>
<<Placeholder>>
}Here's an example of loading in a List of todos from jsonplaceholder.typicode.com:
struct Todo: Codable, Identifiable {
let id: Int
let userId: Int
let title: String
let completed: Bool
}struct ContentView : View {
var body: some View {
NavigationView {
RequestView(Request {
Url("https://jsonplaceholder.typicode.com/todos")
}) { data in
List(data != nil ? try! JSONDecoder().decode([Todo].self, from: data!) : []) { todo in
HStack {
Image(systemName: "checkmark.circle" + (todo.completed ? ".fill" : ""))
Text(todo.title)
}
}
Text("Loading...")
}
.navigationBarTitle(Text("Todos"))
}
}
}RequestImage asynchronously loads an image from the web. You can pass in a Request or just a Url:
RequestImage(Request {
Url("image.example.com/myImage.png")
Header.Authorization(.basic(username: "username", password: "password"))
})
RequestImage(Url("image.example.com/myImage.jpg"))