Sunday 2 July 2017

Manage ViewController

Apple uses MVC pattern very efficiently for developing apps for iOS and OS X. Apple also expects from the developer who are going to develop apps to use MVC efficiently so that they can get the full advantage of MVC and delegate pattern.

Here I am assuming that you all know about what is MVC and how it works in real app development. So I am not going to dive in to elaborate about MVC. Here I will explain how to use it in iOS app development so that one can write readable, scalable and easy to maintain code.

A controller is designed  to take the the request from the view, parse the request and pass data to model for processing and get the result of processing from the model and return back it to view. But a common mistake we do in our development is that we forgot the model and handle all processing in controller results in a bulky controller. We also set the delegate of UITableView and and other UIComponents to controller, this also lead to increase the size of controller

The above described problem leads to unreadable, not able to maintain easily and even unable to understand for a new developer.

Thus how can we overcome from this problem in iOS app development and how can we make our controller skinny?

To get the answer to these question, you can follow these steps:
1) Try to set the delegate separate from the controller.
 For example, a delegate can be any class which can provide the implementation for the set of methods declared in protocol. Thus we can implement data source and delegate for the UITableView in a separate class and assign the reference  of object to datasource and delegate property of UITableView respectively in controller.

class MyDataSource: UITableViewDataSource, UITableViewDelegate{
    
       
        //implement required method here

}

Then in your view controller, first declare the property of type MyDataSource like this:

var myDataSource:MyDataSource!

and  set the datasource and delegate property of UITableVIew like this in viewDidLoad() method:

myDataSource = MyDataSource()
tableView.dataSource = myDataSource
tableView.delegate = myDataSource

2) Don't make any web service call in controller, try to make api call from your model class.
For example, if you want to send the data of user form(user profile details) to your server, for this you can create a user model class and make a method with completion handler (using closures) in model to call the web service to post the data.

3) Follow write less and produce more. Don't write redundant code, like you should avoid of writing the code which is to be used in more than one place.
In swift for this you can use extension, functional programming and protocol oriented programming to write more efficient code.


No comments:

Post a Comment