Example using Async/Await to recover data from online JSON
https://random-data-api.com/api/computer/random_computer?size=4
{
"id": 4749,
"uid": "92933cda-5396-4ab4-ae72-4b61b1651efb",
"platform": "macOS",
"type": "workstation",
"os": "High Sierra (10.13)",
"stack": "Linux, Debian 10.10.3"
}
struct Computer: Identifiable, Codable {
var id: Int
var uid: String
var platform: String
var type: String
var os: String
var stack: String
}
.task {
do {
computers = try await getNetworkData()
} catch let error {
fatalError("\(error)")
}
}
func getNetworkData() async throws -> [Computer]
Image view that asynchronously loads and displays an image.
AsyncImage(url: URL(string: imageURL)) { image in
image
.resizable()
.aspectRatio(contentMode: .fit)
.frame(width: 200, height: 200)
.clipShape(RoundedRectangle(cornerRadius: 30, style: .continuous))
} placeholder: {
ProgressView()
.progressViewStyle(.circular)
.scaleEffect(1.5)
}