Have you heard that MVC is not the best mobile design pattern ? The biggest reason is because it causes massive ViewController. It also doesn’t allow for separation of concerns. All the business logic, network api requests , and UI presentation logic is inside the ViewController. This is why people refer it as a massive ViewController. You ask yourself how to redeem this situation. You can opt to use either a different design pattern or create many classes to separate  the responsibility of each class. For example you can create a NetworkManager class that does all the network connection. You can create a Presenter class for UI Presentation logic and a controller that does all the logic and interacts with Model, NetworkManager.  Lets take a closer look at what MVC is before I get deeper describing how to create an MVVM architecture, let me explain what MVC is:


     MVC is divided into Model, View, and Controller. The Model is responsible to be container for the data and the data can be updated via the Controller. The View is just the screen that includes buttons, Views, and anything UI related. The Controller is the binding between the View and The Model. The Controller calls the network and gets the data and calls the model to update the data .  Once the data is received it reloads the view. The key point to remember is that the Model and The View never talk to each other. The communication between the Model and the View is through the controller.  I


     In the MVVM model the controller is subdivided into ViewModel and the View. In iOS we have a UIViewController. In the MVVM model we create a new class called ViewModel that contains all the calls to network and logic. All the presentation logic stays in the ViewController. Now you ask yourself how do I update the data in the ViewController.  This is achieved by binding the viewModel to the ViewController.

Binding between ViewModel and ViewController

     Create a reference in the ViewController for the viewModel. Then call the viewModel from the viewController. For binding you can either use closures and receive the data in a success closure or you can create a protocol inside the viewModel that has a method declaration : didSucceedWithData(data:Model). At this point make the  ViewController conform to that protocol. When the data is received the protocol method didSucceedWithData will be called and data will be passed. At this moment you can reload the tableview or the View. 


Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>