how can I wait for the result of an async function that loads data for storekit2 in swiftui?
Swift UI Code:
struct SettingsForm : View {
@State var selectedRegion: Int = 0
@State var alwaysOriginalTitle: Bool = false
@State
private var products: [Product] = []
@Environment(\.presentationMode) var presentationMode
let productIds = ["premium"]
init() {
loadProducts()
}
private func loadProducts() async throws {
self.products = await Product.products(for: productIds)
print(self.products)
}
...
You just need to check if your products are loaded, and if not, show for example a ProgressView
struct SettingsForm : View {
@State private var products: [Product]?
let productIds = ["premium"]
var body: some View {
if let products {
// Display list of products
} else {
ProgressView("Loading products")
.task {
do {
try await loadProducts()
} catch {
// handleError
}
}
}
}
func loadProducts() async throws {
self.products = try await Product.products(for: productIds)
}
}