Hey,
I am currently struggling with this issue where I just can't wrap my head around. First I want to explain my goal. After completing a certain task within my view model, I want to trigger an alert popup. For this I have a @Published property which is just a basic enum containing .success and .failure which will be set after the task is done. On my view side I basically want to listen to that change and therefore trigger a state property called showAlert which the triggers the alert.
VStack {
loadingView
}
.onChange(of: viewModel.alertType) { type in
if type != nil {
showAlert = true
}
}
.alert(
alertTitle(for: viewModel.alertType),
isPresented: $viewModel.isPresentingAlert,
presenting: viewModel.alertType
) { type in
alertActions(for: type)
} message: { type in
Text(alertMessage(for: type))
}
.onAppear {
viewModel.onAppear()
}
My View Model contains these properties:
@Published
var alertType: AlertType?
@Published
var isPresentingAlert = false
During debugging I figured out that the .onChange doesn't get triggered at all. So I started commenting pieces of my code and figured out that the issue is in the .onAppear modifier. I really can't wrap my head around what the issue is here.
To me thats a strange behavior. Can someone help here?