Skip to content

ViewModels reinitialize after view changes #50

@guillevc

Description

@guillevc

First of all, thanks for all the work put onto this project. I'm building a project based off of this and it's looking really good, but I am encountering some issues that I explain below.

To summarize:

ViewA navigates to ViewB. To achieve this, I implement a NavigationLink(destination: ViewB(viewModel: ViewBViewModel())). When inside ViewB, some app state is modified, which ViewA receives with a onReceive. In the onReceive callback, ViewA makes some changes to its view like disabling a button. This causes ViewA to recalculate itself, which initializes ViewBViewModel again, losing all the changes, including all loaded data from the repository, leaving the user on ViewB with all Loadable set to .notRequested, and all viewModel state lost.

This also happens if the app state is implemented through a EnvironmentObject. If I modify some environment object from ViewB that ViewA uses, ViewA recreates itself, losing all the state on ViewBViewModel.

I am also having some issues related to this when navigating back from a view to its predecessor. In some cases, if I navigate from some view ViewA to other view ViewB, when dismissing ViewB, ViewA gets recalculated, which initializes ViewBViewModel again.

Has anyone encountered this behaviour and came to a solution?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions