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

[SwiftAveiro] - MVVM World Tour 2016 by Rui Peres

[SwiftAveiro] - MVVM World Tour 2016 by Rui Peres

In this talk Rui Peres (@peres) will discuss the Why and the How in MVVM. Finally why things brake independently of the Architecture chosen and what we can do differently to improve our apps

8116ac7e25a02ba3305c7608378caf6e?s=128

SwiftAveiro

March 20, 2016
Tweet

Transcript

  1. None
  2. !

  3. MVVM WORLD TOUR 2016

  4. WHO IS USING MVVM? ✋

  5. AGENDA 1. What is MVVM? ! 2. What do you

    get with MVVM (versus MVC)? ☺ 3. FAQ # 4. Conclusion ✨
  6. 1. WHAT IS MVVM? !

  7. MVVM IS AN ARCHITECTURAL PATTERN

  8. None
  9. None
  10. None
  11. None
  12. WHAT DOES UPDATES MEANS? !

  13. DELEGATION?

  14. !"

  15. MVVM + DELEGATION == GLORIFIED MVC

  16. 1. Notifications ! 2. KVO " 3. Anonymous Functions #

  17. WHAT DOES MICROSOFT, THINKS ABOUT THIS?

  18. BINDER !

  19. Declarative data- and command- binding are implicit in the MVVM

    pattern.1 1 https://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93viewmodel
  20. When implemented outside of the Microsoft stack the presence of

    a declarative databinding technology is a key enabler of the pattern.2 2 https://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93viewmodel
  21. WHAT DO YOU GET WITH MVVM (VERSUS MVC)? ☺

  22. COMPONENTS ARE NORMALLY MORE REUSABLE, SINCE THEY ARE DECOUPLED FROM

    WHO OWNS THEM !
  23. TESTING BECOMES EASIER !

  24. TENDS TO HAVE A BETTER SEPARATION BETWEEN LAYERS ✨

  25. None
  26. BUT...

  27. MVVM without a FRP inspired framework is only a small

    improvement over MVC: it still suffers from the same problem. — Me, just now
  28. It doesn't help dealing with flow complexity and state management

    at a fundamental level. — Me, again
  29. 2. FAQ !

  30. "I have been hearing a lot about MVVM with Views

    and ViewModels, but where's the UIViewController now? What's its role?" — Maria João Pires
  31. THIS MIGHT SHOCK YOU, BUT... !

  32. The UIViewController's role is the same as it was in

    MVC... !
  33. ▸ Part of the View layer ▸ No Business Logic

    ▸ No Model layer Logic ▸ Normally not reusable
  34. "With MVVM in mind, how would you deal with cells?"

    — Fernando Pessoa
  35. EACH HAS ITS OWN VIEWMODEL !

  36. UITableViewDataSource func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let viewModel = ... let cell = ... cell.viewModel = viewModel return cell }
  37. Cell var viewModel: CellViewModelRepresentable? { didSet { // } }

  38. protocol CellViewModelRepresentable { var title: String { get } var

    description: String { get } func fetchImage(callback: UIImage -> Void) func fetchUpvotes(callback: Int -> Void) }
  39. didSet { titleLabel.text = viewModel.title descriptionLabel.text= viewModel.description viewModel.fetchImage {[weak self]

    image in self?.imageView.image = image } viewModel.fetchUpvotes {[weak self] upvotes in self?.upvotes.text = String(upvotes) } }
  40. "I have seen ViewModels with localized strings, sometimes with Colors,

    other times interacting with the Model layer. What's really the role of the ViewModel? Is it supose to do all this?" — Guilhermina Suggia
  41. (...) the view model is responsible for exposing (converting) the

    data objects from the model in such a way objects are easily managed and consumed. 3 3 https://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93viewmodel
  42. IT'S PERFECTLY VALID TO PASS A DUMMY VIEWMODEL WITH COLORS

    AND LOCALIZED STRINGS.
  43. ITS THE VIEWMODEL JOB TO PROVIDE THE VIEW THE DATA

    IT NEEDS
  44. "All the examples I have seen, revolve around the ViewModel

    interactions with the Model layer, does the ViewModel have anything to say about UI flows?" — Carlos Paião
  45. I DON'T HAVE A GOOD ANSWER FOR THIS...

  46. IDEALLY THE VIEW WOULD HAVE SOMETHING TO MANAGE THIS... !

  47. None
  48. "Does it make sense for one View to have more

    than one ViewModel? What about the opposite? One ViewModel serving more than one View?" — Luísa Todi
  49. Will I finally get ride of my Massive ViewController with

    MVVM? — Sequeira Costa
  50. MASSIVE VIEWMODEL? !

  51. !

  52. "Where does MVVM sucks? " — Eça de Queirós

  53. COMPLEX UI'S WITH MANY LEVELS !

  54. POTENTIAL SOLUTION?

  55. VIPER? !

  56. FLUX/REDUX !

  57. 4. TO CONCLUDE... ✨

  58. MVVM IS NOT A RADICAL CHANGE FROM WHAT WE ARE

    USED TO
  59. MVVM IS NOT A SILVER BULLET AND YOU SHOULDN'T BLINDLY

    FOLLOW THE CURRENT CARGO CULT AROUND IT
  60. WHAT MAKES MVVM AWESOME IS ITS SYNERGY WITH FRP FRAMEWORKS

  61. FINALLY...

  62. ARCHITECTURAL PATTERNS COME AND GO, BUT GOOD ENGINEERING PRINCIPLES STAY

    4 4 SOLID principles
  63. Q&A ! Rui Peres (@peres)