updating @State variable in swiftUI not cause to reload when using delegate which called from outside

162 Views Asked by At

I have this code:

import SwiftUI

@main
struct MyWatchApp: App {
    
    @State var myEnum: MyEnum = .first

    var body: some Scene {
        WindowGroup {
            getCorrectView()
        }
    }

    func getCorrectView() -> some View {
        DispatchQueue.main.asyncAfter(deadline: .now() + 3) {
            self.update(.second) // this works
        }

        switch myEnum {
            case .first: return AnyView(Text("First"))
            case .second: return AnyView(Text("Second"))
        }
    }
}
extension MyWatchApp: MyDelegate {
    func update(_ newState: MyEnum) {
        // when this func calls from outside, this print called but view not reloads
        // when this func calls from my getCorrectView, works as expected
        print("state updated")
        myEnum = newState
    }
}

My problem is that calling "update" function from other class, calles method, even print() called but view not reloads like when this function called from inside this file.

0

There are 0 best solutions below