Calling Async Functions in SwiftUI

Hi,

I'd like to call an Async function upon a state change or onAppear() but I'm not sure how to do so. Below is my code:

.onAppear() {
    if !subscribed {
         await Subscriptions().checkSubscriptionStatus()
    }
}

class Subscriptions {
    var subscribed = UserDefaults.standard.bool(forKey: "subscribed")
    
    func checkSubscriptionStatus() async {
        if !subscribed {
            await loadProducts()
        }
    }
    
    func loadProducts() async {
        for await purchaseIntent in PurchaseIntent.intents {
            // Complete the purchase workflow.
            await purchaseProduct(purchaseIntent.product)
        }
    }
    
    func purchaseProduct(_ product: Product) async {
        // Complete the purchase workflow.
        do {
            try await product.purchase()
        }
        catch {
            // Add your error handling here.
        }
        // Add your remaining purchase workflow here.
    }
}
Calling Async Functions in SwiftUI
 
 
Q