MVVM in SwiftUI
MVVM in SwiftUI
What is MVVM?
MVVM stands for Model-View-ViewModel. It is a design pattern used in software development to separate the user interface (View) from the business logic and data (Model) through an intermediary (ViewModel). This separation helps in managing complex applications, improving testability, and enhancing maintainability.
Model
The Model represents the data and business logic of the application. It is responsible for managing the data, whether it comes from a database, a web service, or any other source.
TodoItgem.swift
1 | import Foundation |
ViewModel
The ViewModel acts as a bridge between the Model and the View. It retrieves data from the Model, processes it if necessary, and exposes it in a way that the View can easily consume.
1 | import Foundation |
Here we define a ViewModel class that conforms to ObservableObject. It has a published property itemList that holds an array of TodoItem. The addItem method adds a new item to the list.
@Published is a property wrapper that allows SwiftUI views to automatically update when the property changes.
ContentView has addItem method to add new items to the list.
View
The View is responsible for displaying the user interface. It observes the ViewModel and updates the UI when the data changes.
Content-ContentView.swift
1 | import SwiftUI |
In the view, we create a ViewModel with @StateObject. ViewModel is observed by the view, and when the itemList changes, the List will automatically update to reflect the new data. The Button calls the addItem method in the ViewModel to add a new item to the list.
@StateObject vs @ObservedObject
@StateObject is used to create a single instance of a ViewModel that is owned by the View. It is initialized only once during the lifecycle of the View.
@ObservedObject is used to observe an existing instance of a ViewModel that is owned by another View. It does not create a new instance but rather listens for changes in the existing one.
Difference:
- Use
@StateObjectwhen the View is responsible for creating and owning the ViewModel - Use
@ObservedObjectwhen the View is receiving the ViewModel from another source, such as a parent View. - In this example, we use
@StateObjectbecause theContentViewis responsible for creating and owning theViewModel.