'swiftUI modify state var from remote closure

I've got an application written in swift/swiftUI.

the logic part composed of events generator that enable the client to register a callback for each event before the UI part starts.

init.swift
-----------

func applicationDidFinishLaunching(_ aNotification: Notification) {

    let event1Handler : eventHandler = {(data) in
        ContentView().alert_handler.showAlert = true
    }
    let event2Handler : eventHandler = {(data) in
        ...
    }

    eventGenerator.sharedInstance().registerUIEvent1(event1Handler, event2: event2Handler)

    window = NSWindow(...)
    window.contentView = NSHostingView(rootView: ContentView())
...
}

in the UI part, there's an optional alert that is presented depent on the showAlert that can be set from the closure from the previous file...

class alertHandler: ObservableObject {
    @Published var showAlert = false
}

struct ContentView: View {
     @ObservedObject var alert_handler = alertHandler()
     var body: some View {
         GeometryReader { metrics in
         .....
         }.alert(isPresented: $alert_handler.showAlert, content: {
            Alert(title: Text("Alert:"),
                    message: Text("press OK to execute default action..."),
                    dismissButton: Alert.Button.default(
                          Text("Press ok here"), action: { print("Hello world!") }
          )
      )
    })

Unfortunately I couldn't see the alert appears when the first event was triggered. Perhaps anyone can tell me if I'd doing anything wrong ? or suggest an alternative approach to modify swiftUI struct variable (event_handler) from remote closure ?

I believe that my the problem may derived from ContentView module which is not a singleton, so when I set showAlert I'm doing so for another instance of ContentView. How can I fix my code to access showAlert that belongs to the currently running instance of ContentView ?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source