Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Off the Rails: Why It's Time to Move Beyond MVC...

Off the Rails: Why It's Time to Move Beyond MVC for Cocoa Development

These are my slides from the May 7, 2014 NSMeetup in San Francisco.

Adam Block

May 08, 2014
Tweet

Other Decks in Programming

Transcript

  1. Off the Rails: Why It’s Time to Move Beyond “MVC”

    for Cocoa Development ! Adam Block CTO & Co-founder, Breeze www.breezeworks.com
  2. “A view controller manages a set of views that make

    up a portion of your app’s user interface.” — Apple, UIViewController Class Reference
  3. “A view controller manages a set of views that make

    up a portion of your app’s user interface.” — Apple, UIViewController Class Reference “View controllers are traditional controller objects in the Model-View- Controller (MVC) design pattern” — Apple, About View Controllers
  4. In or Out? • Code directly related to high-level views:

    IN • Domain logic: OUT • Complex view code: OUT • UI validation: OUT • Formatters: OUT • Data store: OUT
  5. Model-View-ViewModel • Implementation of Martin Fowler’s Presentation Model “The essence

    of a Presentation Model is of a fully self-contained class that represents all the data and behavior of the UI window, but without any of the controls used to render that UI on the screen. A view then simply projects the state of the presentation model onto the glass.”
  6. • Exposes all of the fully-composed data to view •

    Generally, strings and enums • Updates model code in response to commands sent from the view
  7. Yum! • Very clear which layer you’re working with •

    Separation of Concerns is easy to enforce • VMs can’t know about VCs! • VCs are much shorter and the routing is clearer • VM code is reusable and decomposable • Testable, finally
  8. Ptooey! • Can be overkill for simple screens • VMs

    themselves can get pretty hairy • VM interface design often ends up being iterative • Bindings • Don’t work with collections or primitives • KVO requirement can be painful • Debugging is declarative and hunting down bugs isn’t always easy
  9. Routing • Who owns this? • Controller layer went bye-bye

    • MVCVM? • Apple makes it hard to have it anywhere but in the UIViewController • Or have the VC query the ViewModel for routing
  10. UITableView • Sort of a mess • UITableViewDelegate and UITableViewDataSource

    mix up View and ViewModel code • Option 1: Selectively forward delegate and data source methods to ViewModel • Option 2: Have the VM be the data source and forward delegate methods as needed
  11. UITableView methods: In or Out? - tableView:didSelectRowAtIndexPath: - tableView:viewForHeaderInSection: -

    numberOfSectionsInTableView: - tableView:cellForRowAtIndexPath: - tableView:numberOfRowsInSection: - tableView:titleForHeaderInSection: - tableView:heightForRowAtIndexPath:
  12. UITableView methods: In or Out? - (void)tableView:didSelectRowAtIndexPath: - (UIView *)tableView:viewForHeaderInSection:

    - (NSInteger)numberOfSectionsInTableView: - (UITableViewCell *)tableView:cellForRowAtIndexPath: - (NSInteger)tableView:numberOfRowsInSection: - (NSString *)tableView:titleForHeaderInSection: - (CGFloat)tableView:heightForRowAtIndexPath:
  13. UITableView methods: In or Out? - (void)tableView:didSelectRowAtIndexPath: - (UIView *)tableView:viewForHeaderInSection:

    - (NSInteger)numberOfSectionsInTableView: - (UITableViewCell *)tableView:cellForRowAtIndexPath: - (NSInteger)tableView:numberOfRowsInSection: - (NSString *)tableView:titleForHeaderInSection: - (CGFloat)tableView:heightForRowAtIndexPath:
  14. Best Our Practices • Use bindings (MVVM) • Use flag

    properties if needed • Don’t use UITableViewController (like, ever) • Don’t set ViewModel as UITableView dataSource • Forwards most methods delegate and dataSource methods • UITableViewCells are View + ViewModel • ViewModel owns fetchedResultsController
  15. TL;DL(isten) • UIViewControllers aren’t controllers, they’re views • Free your

    VCs from business logic, formatters, validation, storage, and complex view hierarchies • Use a ViewModel or Presenter to represent the state of your UI abstracted from view code • Bindings simplify links between layers, but aren’t the only way that works • Experiment to figure out what works best for your team
  16. References • http://tomdalling.com/blog/software-design/model-view-controller-explained/ • http://joel.inpointform.net/software-development/mvvm-vs-mvp-vs-mvc-the-differences-explained/ • http://www.objc.io/issue-1/lighter-view-controllers.html • https://speakerdeck.com/trianglecocoa/unburdened-viewcontrollers-by-jay-thrash •

    http://cocoasamurai.blogspot.com/2013/03/basic-mvvm-with-reactivecocoa.html • http://www.sebastianrehnby.com/blog/2013/01/01/skinnier-controllers-using-view-categories • http://inessential.com/2012/12/31/uitableviewcell_is_not_a_controller • http://chris.eidhof.nl/posts/lighter-uiviewcontrollers.html • http://martinfowler.com/eaaDev/PresentationModel.html • http://msdn.microsoft.com/en-us/magazine/dd419663.aspx