Break up massive view controllers (Swift)

Introduction

MODEL-VIEW-ADAPTER DESSING PATTERN. It is quite common in iOS programming that view controllers begin to grow and at the middle of development you discover that you have a view controller class with hundreds or even thousands of line. This is the common scenario where the view controller, apart from dealing with its controllers (uitableviews, uitableviews, hierarchy of views,…), has also to process data for being properly presented attend to notifications.

 

The aim of this post is to explain an approach for breaking up view controllers into different design classes and provide them different responsibilities for practically leave the view controller with the responsibility of setup its view controller and filling its ui controllers when necessary.

Class design

MVC approach

MVC

This approach is the most convenient case that the amount of information that you have to present in the view is quite reduced.

MVA approach

mva

 

This other is the most convenient when you have to deal with more than one dataSource in the same view controller, and also the origin of the data to show come from different sources (a data base, internet request, some costing data process,…).

Responsibilities:

View. As always, show the content views.

View Controller. In this approach, setup its uicontrols (tableviews, collection views, a view hierarchy, …) and attend its events. This is not something new, but what is new is that is responsible of being delegate of an Adaptor class and implement its delegated methods. Those delegated methods are basically informing view controller that there is new data to show in the view.

ModelView. Is the model data to be shown in the view.

Adaptor. Is the responsible for starting the processes for retrieving data and providing it to Model Data. Once there is some data to show is provided to viewcontroller via delegate.

AsyncOperation1,…n. Are asynchronous operation for retrieving data from elsewhere. In the worst case scenario this action will take time.

As you can see the View Controller get slimmer by moving part of the code that it performed in MVC approach to Adaptor and AsyncOperation classes.

The template

For proving you that concept you can take this sample project that implements what I have exposed.

 

 

3 thoughts on “Break up massive view controllers (Swift)

  1. Way cool, some valid points! I appreciate you making this article available, the remaining
    part of the website is also high quality. Have a
    enjoyable.

Leave a Reply

Your email address will not be published.