.onAppear and .task code not running

Hi,

I have the following code, which for some reason is not working as expected. I have an .onAppear and a .task function that isn't running, which I can see isn't running because nothing is printing. Any guidance would be greatly appreciated. Thank you.

struct ContentView: View {
var body: some View {
ZStack {
switch view {
case .view1: View1()
case .view2: View2()
case .view3: View3()
case .view4: View4()
case .view5: View5()
default: SubscriptionStoreView(groupID: "")
}
}
.onAppear() {
view = .view6
print("test 1")
}
.task {
print("test")
await refreshPurchasedProducts()
}
}
func refreshPurchasedProducts() async {
// Iterate through the user's purchased products.
for await verificationResult in Transaction.currentEntitlements {
switch verificationResult {
case .verified(let transaction): print("verified")
case .unverified(let unverifiedTransaction, let verificationError): print("unverified")
default: print("default")
}
}
}
}

Where is view6 in your switch statement?

Where is view defined? I'd rename it to something else: "view" is too generic, and could be mistyped to "View".

Do View1() through View5() actually display anything?

Secondly, it's .onAppear {} not .onAppear() {}.

Hi,

Thanks for your response. I changed the original View names to generic names for the purpose of this post. The view variable is defined in the ContentView struct like this:

@State var view: ScreenView

ScreenView is an enum with the different view names.

Only views 3 and 4 have code - the rest of the views/switch could be commented out.view6 is just the default view so there isn't a case for it in the switch statement. I changed .onAppear() to .onAppear. I don't see anything in the views that could possibly prevent those two functions from running.

.onAppear and .task code not running
 
 
Q