Learning MVVM: for iOS development 🚀

Are you interested in different architectural patterns? Are you stuck on Apple’s MVC pattern and could not find time to learn new architectures? Or you want to write more clean, structured, testable code?

If your answer is YES for all of the questions above, you might be interested in this post.

Image for post
Image for post
“worm's eye view photography of metal frame” by Ricardo Gomez Angel on Unsplash

Let’s start

First thing that I should say: this is not a tutorial. It is just a simple post for motivating myself and being helpful to anybody who are saying yes for the questions that I wrote above.

Why should we choose an architecture?

There are lots of different types of architectures that we can prefer to use such as MVC, MVP, VIPER or MVVM while developing iOS apps(and/or other platforms). There are main purposes for choosing an architecture before starting to develop an iOS project. Developing a project with an architecture:

  • Forces you to write well separated, more readable code,
  • Easier to apply SOLID principles as close as possible,
  • Helps you to write unit tests easier,
  • Helps you to reduce code size of view controllers’ to prevent them to become a Massive View Controller’s.

Why MVVM?

There is no specific reason for me choosing MVVM to learn. But here is a list that why I am choosing it rather than the others:

  • Popular to use.
  • Easy to find good sources for learning.
  • I hear that it is commonly used by lots of popular companies for their featured apps on App Store.
  • Some people say Reactive Programming is future (it has a good relationship with MVVM).
  • I saw that lots of companies are looking for candidates who have MVVM or/and RxSwift/RxCocoa knowledge. Looking at job posts is a good way for following and learning trends for improving your skills.

Learning new architecture is exciting but it’s challenging. Once you are starting to learn MVVM, you will see the “data binding” thing. And you have to choose one of the “data binding” options.

Let’s see what I have done.

Which option to choose for data binding?

Here is an awesome tutorial for learning MVVM from scratch. In this tutorial, the instructor is telling that there are few options to bind the data. The video is about an hour but I am going to summarize it for you:

  1. KVO
  2. Delegation
  3. Functional Reactive Programming (FRP)
  4. Property Observers
  5. Boxing

MVVM is famous for synchronizing data between UI and the model using viewModel’s and you have to bind your data by using one of the options that I listed above.

Conclusion

Best way to learn something new is practicing. So, I decided to make a simple app that fetches data from iTunes API and lists tracks.

Actually, the first part of the demo is over for me. I have made some research about MVVM and looked at a few example projects on GitHub after watching the video that I shared above. I chose the “Boxing” option for data binding as instructor did. Using “Boxing” is a pretty easy option while understanding the basics of MVVM.

Here is the source code:

The first part for me was learning basics of MVVM and it went well for me. I am planning to add some features for improving my knowledge about it:

  • Search & list results
  • Detail screen for showing more data about the track
  • Or just make a basic music player to listen to the preview of the track

If you are interested, you can watch the repository for getting notified about the updates. If this post motivated you to learn or found it useful you could give a 👏 or buy me a coffee😄 💪

Thanks for reading!

Note: If you see anything wrong in this post, please comment so I can update it as soon as possible. Also I will be glad if you make pull requests 🙏

Written by

iOS Engineer at Spotify via HiQ • Outdoor enthusiast 🌳 gokhan.se

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store