Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon What are the main differences between MvvmCross 4.x and 3.x regarding ViewModel lifecycle


What are the main differences between MvvmCross 4.x and 3.x regarding ViewModel lifecycle


MvvmCross versions 3.x and 4.x have several differences regarding the ViewModel lifecycle, primarily centered around how ViewModels are constructed, initialized, and managed.

MvvmCross 3.x

In MvvmCross 3.x, the ViewModel lifecycle follows a sequence known as CIRS: Construction, Init(), ReloadState(), and Start(). This sequence is used to initialize and manage ViewModels. Here's a breakdown:

- Construction: The ViewModel is instantiated using the IoC container, allowing for dependency injection. This is where services and other dependencies are injected into the ViewModel.
- Init(): This method is used to initialize navigation parameters. It is called after construction and is where you would typically set up any initial data or parameters passed to the ViewModel.
- ReloadState(): This method is responsible for rehydrating the ViewModel's state after tombstoning, which occurs when the app is suspended and then resumed due to low memory conditions.
- Start(): This method is called when initialization and rehydration are complete. It is typically used to start any ongoing operations or tasks.

MvvmCross 4.x

MvvmCross 4.x continues to use the CIRS sequence but introduces some changes and improvements:

- Construction remains the same, using IoC for dependency injection.
- Init() still initializes navigation parameters but is now more flexible, allowing for typed navigation parameters.
- ReloadState() continues to handle state rehydration after tombstoning.
- Start() is similar but may be used less frequently due to the introduction of more asynchronous initialization methods.

A significant change in MvvmCross 4.x is the continued support for typed navigation parameters, which allows for more structured data passing between ViewModels. Additionally, MvvmCross 4.x lays the groundwork for future improvements in navigation and lifecycle management, which are further enhanced in MvvmCross 5.x.

Key Differences

- Navigation Parameters: MvvmCross 3.x passes navigation parameters directly to the `Init()` method, while MvvmCross 4.x supports typed navigation parameters, making it easier to pass complex data between ViewModels.
- Lifecycle Flexibility: MvvmCross 4.x provides more flexibility in handling ViewModel lifecycles, especially with the introduction of asynchronous initialization methods.
- Tombstoning Support: Both versions support tombstoning, but MvvmCross 4.x continues to refine this process, ensuring that ViewModels can effectively save and restore their state.

Overall, MvvmCross 4.x builds upon the foundation established in 3.x by enhancing navigation, lifecycle management, and flexibility in handling complex data and asynchronous operations.

Citations:
[1] https://www.mvvmcross.com/documentation/fundamentals/viewmodel-lifecycle
[2] https://stackoverflow.com/questions/49322704/mvvmcross-view-model-lifecycle-restore-not-working
[3] https://stackoverflow.com/questions/15961664/viewmodel-lifecycle-when-does-it-get-disposed
[4] https://stackoverflow.com/questions/71959649/does-mvvmcross-have-a-built-in-message-for-viewmodel-to-tell-view-when-viewappea
[5] https://stackoverflow.com/questions/29372480/mvvmcross-viewmodel-life-cycle
[6] https://www.mvvmcross.com/documentation/advanced/customizing-using-App-and-Setup
[7] https://www.mvvmcross.com/documentation/tutorials/tipcalc/a-note-about-views-and-viewmodels
[8] https://www.ramotion.com/blog/what-is-mvvm/
[9] https://www.mvvmcross.com/documentation/fundamentals/navigation
[10] https://www.mvvmcross.com/documentation/fundamentals/inversion-of-control-ioc
[11] https://www.mvvmcross.com/documentation/getting-started/mvvmcross-overview
[12] https://github.com/MvvmCross/MvvmCross/issues/2738