Problem when working with .onChange and .onAppear modifier

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?

you haven't shown code complete enough for anyone to reproduce your problem. Try to reduce the problem to a minimum (maybe in a new project), then repost if need be. You'll probably find that you'll figure it out yourself by performing the reduction. What does "showAlert" do? What do you mean by "the issue is in the .onAppear modifier"? It doesn't compile, it is never executed, it behaves in some other way you don't expect - what?

Problem when working with .onChange and .onAppear modifier
 
 
Q