Project Architecture MVVM in Swift
Project Architecture MVVM in Swift

Project Architecture MVVM in Swift

Shantaram Kokate
AppleCommunity
Published in
4 min readMar 28, 2019

--

Being a developer you must gone through the project architecture. We will be discuss about MVVM here. It might look familiar if you’ve been using the Model-View-Controller pattern to build Swift applications.

It stands for Model-View-ViewModel

Problem :

Let’s Start with an example :

Traditional MVC

In the above program, Once the MovieViewController has finished loading its view, it performs a network request. It delegates this task to the MobileAPIService class, which is a good start to reduce the responsibilities of the view controller.

The implementation looks less complex and that may be true. It has a few defects. But our concern is Why is the view controller responsible for making a network request?🤔 It’s true that the MobileAPIService helps the MovieViewController with this task, but the MovieViewController still knows that the movies it fetches come from a backend.

You could argue that these details are hidden by the MobileAPIService, but that’s not the point.

Following point should be considered For MovieViewController.

  1. The MovieViewController controls a view.
  2. It should only do for UI rendering except other processing like API call, local database handling.
  3. It shouldn’t know where the movies come from.
  4. It should only need to ask for content to display in its view.
  5. It shouldn’t even know about the MobileAPIService. let alone how it does its job.
  6. In a client program (our case MovieViewController) there is must minimal code, which lead to less bug.

Unfortunately, we did not implement the above point in program 1.0 😢

Solution :

MVVM

MVVM pattern as a slightly more complex implementation of the MVC pattern, take a minute and think 🤔 about the possible advantages MVVM has in this above implementation.

In program 1.0 implementation is so simple as compared to the program 1.1 and that might be true.

The ViewController of MVC implementation also manage state, instance of newsList, MobileAPIService and more. This is not a bad or good, but in MVVM It hides these details in ViewMode

What’s new in MVVM implementation :

  1. MVVM pattern hide movies, state, MobileAPIService details in ViewModel.
  2. It discovered ViewController is only responsible for UI controls.
  3. ViewController is not concerned with an array of movies. even it’s doesn’t know a source of movies like via remote, local storage. they might come from backend or local storage managed by core data, SQLite.
  4. Unit testing and testability improve.
  5. It hides these details in ViewModel

What Makes Model-View-ViewModel Great

  1. The first benefit of MVVM you experience is how much code you can remove from your view controllers.
  2. View models are lightweight objects that sit between the model layer and the controller layer.
  3. Your view controllers do not communicate directly with the model layer of your application. View models feed your view controllers with data and provide an interface to interact with the model layer.

Unit Testing

By moving the business logic(data manipulation) to view model. Testing our application easier. and testing view model is easy because it doesn’t hold the client program reference.

The view controller is not depended on the model layer so it’s easy to test.

Task Separation

By adding the View model, View model translates data of data layer into something (eg.model)that view use.

The view controller is not responsible for this task management.

Please find complete source code

If you like this post, please share and give claps so others can find it 👏👏

You can follow me on Medium for updated articles. Also, connect with me on LinkedIn, Twitter.

If you have any comment, question, or recommendation, feel free to post them in the comment section below!👇

--

--