MVVM in iOS Development with Protocol + Closure + Reactive Programming(RxSwift)
Yusuf is an iOS developer who has just started his career in iOS development. He starts building apps with MVC as Apple suggests. After a while, he wishes to go deeply through the development. According to his researches, other developers use MVVM with various architecture extensions. While some of them prefer MVVM + Protocol, others want to use Closure or Reactive Programming with MVVM. Let’s help Yusuf to find out what kinds of differences exist in these approaches in a project.
What is MVVM?
It’s the architecture that simply brings View Model over MVC. The idea behind it that moving logical conditions into the view model from the controller. Is it easy, isn’t it? To be honest, yes. Yusuf is just supposed to create one class which is called view model.
Before going to the project deeply Yusuf must know these,
- There must be only business logic in the view model. Don’t import UIKit ever!
- The view model must be eligible to test. Pass the view model via injection through the init function of the controller.
- Service calls must be in the view model.
Yusuf has created a cryptocurrency ticker project which includes 2 screens. The list screen shows the list of cryptocurrencies with searching support. The detail screen shows the detail of the selected coin in the list screen.
MVVM + Protocol
When the screen appears the refreshCoins function where is in the view model is called, from the viewDidAppear function. In the refreshCoins function, the dummy coins are fetched. Imagine that Yusuf is fetching the coins from the server here. Afterward, the coinsDidRefresh function of the List Delegate is called to pass the fetched coins to the controller. The controller listens to the view model with coinsDidRefresh and coinsCouldNotRefresh functions and takes an action by the result.
MVVM + Closure
In the closure structure, the coinsDidRefresh and coinsCouldNotRefresh closures are listened in the viewDidLoad function in the controller, to take any action. The structure almost the same as in Protocol.
MVVM + Reactive
Here we come to the fancy part. Yusuf uses RxSwift to code reactive on this project. Let’s start with the view model instead of the controller for now. Reactive programming is based on publishing and listening. Listeners are called Observer, those who are listened are called Subject. In the view model, we have the coins property as a BehaviorRelay, which means the observers listen to it to get the latest coin array to update the UI. The BehaviorRelay is a wrapper for BehaviorSubject. Yusuf can assume it is a subject. The main difference is that BehaviorRelay can’t accept error state unlike BehaviorSubject. As we see in the refreshCoins function, the coin array passes through the coins property via the accept function which RxSwift provides for us.
When we look at the viewDidLoad function in the controller, we see some bind and subscribe things. The binding or subscribing is listening to a subject. At the 41. line, if an array passes through the coins property in the view model, the bind function is called and 43. and 44. lines are run by creating a table cell.
As we see in the examples, using protocol and closure are based on almost the same approach. Protocol function or closure is called and the controller gets notified of data changes. Yusuf has to call data every time after he gets notified. Despite protocol and closure, reactive programming is based on data. Yusuf just listens to data in the view model. If data changes, he changes the UI as well. He doesn’t have to directly call data every time.
You can download the project here.