MVVM architecture for iOS

Smriti Arora
Technology at Nineleaps
4 min readJan 7, 2021

--

Firstly, What is the architecture for an app? An architecture is basically how you bring together the building blocks of your app i.e. the program files, code and it’s distribution, so it is well readable, understandable, logical, and most importantly modularly reusable.

Image Source: https://thumbs.gfycat.com/ActiveWarlikeCopperbutterfly-max-1mb.gif

This article will give you a good idea about “MV(X)” ios architectures majorly being MVC and MVVM.

What is MVC?

MVC stands for Model View Controller. It has been one of the most popular as well as easy-to-understand architectures being used for developing iOS applications.

  • Models are basically classes that hold data. These can include codable files, network files, constant files, Core data models, other data sources, etc. In my opinion, the best way to model data is by using structures. One might have a question in mind, that classes can also do that. But I would like to mention, that classes contain a lot more potential than simple tasks like this. Say, when modelling data we don’t need a lot of heavy weightlifting concepts like inheritance, we just need a way of value storage. And most importantly, structures are value-type which means when we copy a struct it shall keep unique copies for both instances unlike classes, which makes more sense when we want to do a task like holding data. That’s why I prefer structs over classes in this case.
  • Views are interacting layers, which take inputs from the user as well as shows do specifically all the “visual tasks” in the app. They don't have to have any business logic, just showing to the user and taking from the user. These include elements such as basic labels, buttons, images to custom complex views, animation, graphics etc.
  • Controllers are how they sound. They control the flow of data. It takes all the data from models does the business logic and passes it to the UI elements.
MVC block diagram.

One must have a question in their minds that, since MVC has been popular as well as an effective way of managing and writing code so why do we have to adapt to a new architecture? It is an undeniable fact that MVC has been working great but the only problem with it is when the size of your project grows, the view controllers clutter with a lot of code in it. So it becomes very difficult to manage the project, this is when MVVM wins the game.

What is MVVM?

MVVM stands for Model View View-Model. It is one of the structural design patterns used for developing iOS applications.

  • Models are classes or structures that hold data.
  • Views refer to the UI elements which show data to the users and take data from the users.
  • View-Model is the mediatory layer between model and view. It does all the way it sounds i.e. helps in managing and updating data in between the view and the model.

So, this is how it works,

  • Models hold the raw data or information in themselves and feed it to the view model.
  • View model does all the operational work on the data and makes it eligible for being displayed on the screens. Once done, the view model passes the data ready for display to the views.
  • Views are responsible for displaying the data to the user and take inputs from it. Views display the data from view models to the user.
Block Diagram representation of MVVM.

Advantages

  • Management: MVVM offers a well-managed code structure that allows us to remove the cluttered code onto the View controllers.
  • Ease of Testability: Since MVVM divides the code into separate modules which brings ease in testability.
  • Understanding: it offers a better understanding of code again due to it’s proper and simplified division of the code.
  • Reusability: division of code also provides us with the benefit of being able to reuse code especially models and views.

Disadvantages

  • While trying to switch from MVC to MVVM at first, it can take a while to differentiate which code to be placed where i.e. it can be a big overhead to manage so many classes initially.

Here is an example of an app in rxswift developed using MVVM architecture. UsersListWithRx GitHub project.

That’s all for now! I hope this article helped you with your learnings. Please share your feedback in the comments section and applaud the article if you think it’s worth it! ✨✨

--

--

Smriti Arora
Technology at Nineleaps

iOS developer, content creator, music enthusiast ✨✨💫💫