Azam massive controller alt conf header

Taming the Massive Controllers

Watch Mohammed Azam simplify bloated Controllers using the “Lean Controller” methodology!


Introduction

My name is Mohammed Azam, and I work at a coding bootcamp called Iron Yard. At Iron Yard, I teach iOS, and web development (back-end and front-end engineering).

Purpose of Lean Controllers in iOS

What is the purpose of Lean Controllers in iOS?

A typical ViewController connects your model to a view. It can easily end up with thousands of lines of code, contributing to codesmell. The purpose of Lean Controllers is to divide the ViewController and split it up into separate places.

Lean Controllers in Practice

Let’s take for example my controller PokemonTableViewControllerUgly. Inside the viewDidLoad, I’m making a request to a web service that’s written in Swift.

Get more development news like this

override func viewDidLoad() {
  super.viewDidLoad()
  
  let url = URL(string: "https://still-wave-26435.herokuapp.com/pokemon/all")!
  
  URLSession.shared.dataTask(with:url) { data, _, _ in 
  
    let json = try! JSONSerialization.jsonObject(with: data!, options:[]))
    let dictionaries = json as! [[String:Any]]
    
    for dictionary in dictionaries {
      self.pokemons.append(dictionary)
    }
    
    self.tableView.reloadData()
  }.resume()
}

The code is written like so because I have to make the network call, and I also have a UITableViewController along with a segue that takes me into the detailView. In turning this into a Lean Controller, I want to separate the components so that they can be made for reuse.

Demonstration

The first thing we can do to improve the viewDidLoad is to create a method called updateTableView.

Here, we use the resource Pokemon.all, which will return strongly typed Pokemon.


private func updateTableView() {
  Webserivce().load(Pokemon.all) { pokemons in
  }
}

WebService Class

The WebService Class has a load method which takes in a resource structure.

struct Resource<T> {
  let url: URL!
  let parse (Data) -> T?
  let httpMethod: HTTPMethod
}

This resource provides us with the ability to parse the Pokemon data from JSON.

The Pokemon.swift class is the actual model that will be creating the resource.


extension Pokemon {
  static let all = Resource<[Pokemon]>(url: App.Pokemon.all.url) { data in
  
  let json = try? JSONSerialization.jsonObject(with: data, options: [])
  
  guard let dictionaries = json as? [JSONDictionary] else  {
  return nil
  }
  return dictionaries.flatMap(Pokemon.init)
  }
  
  static func byId(id: Int) -> Resources<Pokenmon> {
    return Resources<Pokemon>(url: App.Pokemon.get(id: id).url) { data in
    
  let json = try? JSONSerialization.jsonObject(with: data, options: [])
  
  guard let dictionary = json as? JSONDictionary else . {
    return nil
  }
  
  return Pokemon(dictionary: dictionary)
    
    }
  }
}

When I call Pokemon.all, it will create and return the particular resource, which also gets and parses the JSON, and results in strongly typed objects - a collection of Pokemon.

I’m no longer using hard-coded string URLs. Instead, I’m building the URL so that it can be easily changed later on.

Demonstration Continued (8:40-31:47)

Resources

Some of the resources:

Watch the Apple World Wide Developer Conference 2015 to learn more about segue extensions.

Subscribe to Swift Talk.

I also do a lot of Udemy courses, one of them is a Complete Guide to Lean Controllers.

If you go to my website AzamSharp.com and then click on the courses, those links to the courses are already pre-filled with the promo discount code.

My twitter is @azamsharp.

Q&A

**_Q: What do you think about the architectures of Model-view-viewmodel?

Azam: MVVM is a really nice pattern and it has been used in other communities, particularly .Net.

I think the biggest problem with the MVVM on iOS is that there is no predefined way of doing that.

You can definitely use RxSwift, but there is a steep learning curve for the reactive framework.

Next Up: Building a Unidirectional Data Flow App in Swift with Realm

General link arrow white

About the content

This talk was delivered live in June 2017 at AltConf. The video was recorded, produced, and transcribed by Realm, and is published here with the permission of the conference organizers.

Mohammad Azam

Mohammad Azam has been developing iOS applications since 2010. He worked as a lead mobile developer for VALIC, AIG, Schlumberger, Baker Hughes, Blinds.com and The Home Depot. Of his many personal apps on the app store, Vegetable Tree was featured by Apple. He is currently teaching iOS development for The Iron Yard in Houston.