Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Pops back when updating @ObservableObject #133

Open
undertaker28 opened this issue Apr 6, 2024 · 1 comment
Open

Pops back when updating @ObservableObject #133

undertaker28 opened this issue Apr 6, 2024 · 1 comment

Comments

@undertaker28
Copy link

undertaker28 commented Apr 6, 2024

Hi, I have the same problem, which is described in the link below.
https://forums.developer.apple.com/forums/thread/693137
In short:

  1. I navigate to: LoginView -> #View or LoginView -> ForgotPasswordView
  2. I update the environmentObject in #View or ForgotPasswordView but then I get immediately pushed back to LoginView

Main:

@main
struct ARVisioApp: App {
    @UIApplicationDelegateAdaptor(AppDelegate.self) var delegate
    @StateObject private var userStateViewModel = UserStateViewModel(networkMonitor: NetworkMonitor())
    
    var body: some Scene {
        WindowGroup {
            NavigationViewCoordinator(AuthenticationCoordinator(userStateViewModel: userStateViewModel)).view()
        }
        .modelContainer(for: ARObject.self)
    }
}

AuthenticationCoordinator:

final class AuthenticationCoordinator: NavigationCoordinatable {
    let stack = NavigationStack(initial: \AuthenticationCoordinator.start)
    var userStateViewModel: UserStateViewModel
    
    init(userStateViewModel: UserStateViewModel) {
        self.userStateViewModel = userStateViewModel
    }
    
    @Root var start = makeStart
    @Route(.push) var # = make#
    @Route(.push) var forgotPassword = makeForgotPassword
}

AuthenticationCoordinator+Factory:

extension AuthenticationCoordinator {
    @ViewBuilder func makeStart() -> some View {
        LoginView()
            .environmentObject(userStateViewModel)
    }
    
    @ViewBuilder func make#() -> some View {
        #View()
            .environmentObject(userStateViewModel)
    }
    
    @ViewBuilder func makeForgotPassword() -> some View {
        ForgotPasswordView()
            .environmentObject(userStateViewModel)
    }
}

Could you please tell me how this problem can be solved?
Without Stinsen, using NavigationView and NavigationStack, everything works.

@ashishsj0
Copy link

If you haven't found a solution yet, just make the AuthenticationCoordinator a state object in the struct ARVisioApp.
Try to add init and deinit block, you'll most probably notice it is being created everytime the object updates.
NavigationViewCoordinator(AuthenticationCoordinator(userStateViewModel: userStateViewModel)).view()

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants