How to use RxSwift with MVVM
In these posts I’ll show you how to use RxSwift with MVVM pattern. In the posts I’ll teach you how to add RxSwift to your project. How to use RxSwift with UITableView, validate user input data and how to bind data back and forth between view model and the view. You’ll also learn the difference between BehaviorRelay, Observable and PublishSubject.
All the topics are explained using friends application. Friend application is an iOS app that downloads a list of friends from backend and displays it on the screen. It can also create, update and remove friends from the list. You can clone the repo to get the codes. Remember to checkout the RxSwift branch. I wrote the app some time ago without RxSwift using the MVVM pattern. Now I wanted to see how much the code differs after I refactor it to use RxSwift. Incase you are interested about the basics of MVVM please check my other posts on Model-View-ViewModel with swift.
In the first post I’ll show how to set up the application to use RxSwift. how to use RxSwift with UITableView. You also learn the difference between BehaviorRelay, Observable and PublishSubject. You’ll learn about the rx extension and how it helps to bind values to UI components, mostly those related to UITableView.
In the second post we’ll dive in a bit deeper in the relationship between the view model and the view. We’ll check how to validate user input data from multiple components, and how we can easily bind that information to change state in the UI. We’ll also continue with rx to see how to bind the values to UI components other than tableview.
In the third post I’ll explain how to handle networking when using RxSwift in the project.
In the last post we’ll unit test the app we have just created. You’ll learn how to handle Observables in testing, mock network and data and to use dependency injection.
All the codes are available in GitHub so you can explore the codes already, just remember to checkout the RxSwift branch. I am planning to finish the post in upcoming months. Stay tuned! :)
I hope I can help you with RxSwift and that you like the posts. To me Ray Wenderlich RxSwift book was extremely helpful, so I encourage you to read it too. It really helped me to understand these concepts! In case you any questions, comments or suggestions, please comment or DM me on twitter!