Am I right in thinking it's almost like a wrapper for all the objects necessary for a View?
For example, say you had an online store that sold music and dvds. On your browse page you'd want to display a list of all your dvds and music. Would you therefore construct a ViewModel object that has two properties containing an albums list and a dvds list?
From my understanding it seems that you have all your model classes ie. an Album/Dvd class, but simply passing these alone wouldn't be enough for your View. Does a ViewModel basically act as a carrier for all the data your View requires?
The ViewModel class is designed to store and manage UI-related data in a lifecycle conscious way. The ViewModel class allows data to survive configuration changes such as screen rotations.
A model is usually more closely related to how your data is stored (database, services, etc.) and the model will closely resemble those. The ViewModel on the other hand is closely related to how your data is presented to the user. It is usually a flatten version of your model, denormalized, etc.
What is ViewModel? ViewModel in the MVC design pattern is very similar to a "model". The major difference between "Model" and "ViewModel" is that we use a ViewModel only in rendering views. We put all our ViewModel classes in a "ViewModels" named folder, we create this folder. Understand it with an example.
ViewModel classes are used to store the data even the configuration changes like rotating screen. ViewModel is one of the most critical class of the Android Jetpack Architecture Component that support data for UI components. Its purpose is to hold and manage the UI-related data.
Your understanding is mostly correct, but it's not complete.
The ViewModel may also perform conversions from the type of data that your Model carries to the type of data your View can conveniently work with; this might even mean that the ViewModel does not carry Models directly but other vessels that carry (possibly a subset of) the same information in a more suitable format.
Consider that you could have a Library model that aggregates Albums and DVDs -- the difference between such a model and the corresponding ViewModel is precisely that the Model doesn't care (or even know about) the View while the ViewModel has the express purpose of facilitating it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With