iOS Architecture
Essential
Jiakai(Jacky) Lian
Mobile Developer @ GoGet
About Me
❖ Focus on mobile development since
the era of 2G
❖ Master of Applied IT @ Monash
❖ Not a full stack developer
❖ Focus on iOS in recent 2 years
❖ Recently Joined GoGet CarSharing
❖ LinkedIn:
https://au.linkedin.com/in/jacky-lian-
06493379
❖ GitHub: https://github.com/jiakai-
lian/
Purpose of Architecture
Design
❖ Resolve real business problems
❖ Remove unnecessary dependencies
❖ Reduce maintenance cost
❖ Prepare for changes
3 Main Roles in Architecture
❖ Data Owner
❖ Data Processor
❖ Data Consumer
Everything starts from MVC
❖ MVC: Model+View+Controller
❖ MVC in Web programming
❖ View Container: Web Browser
❖ Model: Model Layer
What Apple promised
The Reality
❖ MVC = Massive View Controller
❖ View Controllers have too many responsibilities: Networking, Data Access,UI
configuration, Data Source, Delegate …
❖ Shared Data causes problems (Mutability Management)
❖ Controllers are the least reusable classes in the project
❖ “Cocoa MVC is the best architectural pattern in terms of the speed of the
development.”
Slim Model V.S. Fat Model
❖ Fat Model
❖ Business Logic in Model
❖ Slim Model
❖ Model + Helpers
❖ MVC-S: MVC+Data Store(Data Access)
❖ MVC-N: MVC+Networking
The True MVC in Mobile:
MVP
❖ MVP: Model+Passive View+Presenter
❖ All UI logic in presenter.
❖ View knows nothing about Model
MVVM
❖ MVVM = Model+View+ViewModel
❖ ViewModel Definition
MVVM:View Model
❖ Non-Pattern ViewModel: ViewData/Transition Data (Immutable)
❖ ViewModel in MVVM: Independent Presentation Logic
MVVM: Where is the
Controller
❖ MVVM = M-VM-C-V = Model ViewModel Controller View
❖ Controller bind View and ViewModel together
Benefits of ViewModel
❖ No direct dependency between Model & View
❖ UI Independent Presentation Logic
MVVM With Binding
❖ Fine without binding
❖ Even better with binding
❖ Popular Binding Frameworks: ReactiveCocoa(ObjC &
Swift), RxSwift
❖ Reactive : Learning Cost ⬆
❖ Binding: Debugging Difficulty⬆
VIPER
❖ VIPER = View+Interactor+Presenter+Entity+Router
❖ Interactor: Business Logic, using external data access layer
❖ Presenter: UI related (but UIKit independent) business logic
❖ Entities: Slim Model (Plain Data Objects)
❖ Router: Routing between different modules
VIPER: Highlights
❖ Evenly distribute responsibilities
❖ UseCase focused
❖ Able to combine different UI protocols to build up a screen
VIPER: Problems
❖ Too many classes: Development/Maintenance Cost ⬆
❖ Too complex for simple apps: Slow down the development
VIPER: Suggestions
❖ Workload Reduce: Use Script/Template to generate all those
classes
❖ Not a practical choice for s/m projects
❖ A option for large projects (Team Size ≈ 5/Development Life Cycle ≥
6 month)
Ziggurat
❖ Created by Square
❖ Inspired by React Native & Flux
Ziggurat
❖ More layers distribute
responsibility evenly
❖ The service layer
encapsulates mutability
❖ Presenter & Renders are
stateless
❖ One Way Data Flow
❖ Repository = Data Store
❖ View Model = View Data
❖ Drawback: Relatively hard to
manage animations
Rules Of Thumb
❖ Dependency is the No.1 concern
❖ More layers = Less coupling = Harder to understand
❖ No right or wrong architecture
❖ Plan well then improve your architecture iteratively
Massive Class Prevention
❖ No more than
300 lines of code
Future Topics
❖ Architecture Design By Practice
❖ Protocol Oriented Design in Swift
❖ iOS Animation Design By A Live Example/Examples
References
❖ http://casatwy.com/iosying-yong-jia-gou-tan-viewceng-de-zu-zhi-he-diao-yong-fang-
an.html
❖ https://www.objc.io/issues/13-architecture/viper/
❖ https://www.objc.io/issues/13-architecture/mvvm/
❖ https://medium.com/ios-os-x-development/ios-architecture-patterns-
ecba4c38de52#.tlnbspmx8
❖ https://www.raywenderlich.com/62699/reactivecocoa-tutorial-pt1
❖ https://www.youtube.com/watch?v=4cP1p5VOrSI
❖ https://corner.squareup.com/2015/12/ziggurat-ios-app-architecture.html
❖ http://khanlou.com/2015/12/mvvm-is-not-very-good/
❖ http://limboy.me/ios/2015/09/27/ios-mvvm-without-reactivecocoa.html

iOS Architecture

  • 1.
  • 2.
    About Me ❖ Focuson mobile development since the era of 2G ❖ Master of Applied IT @ Monash ❖ Not a full stack developer ❖ Focus on iOS in recent 2 years ❖ Recently Joined GoGet CarSharing ❖ LinkedIn: https://au.linkedin.com/in/jacky-lian- 06493379 ❖ GitHub: https://github.com/jiakai- lian/
  • 3.
    Purpose of Architecture Design ❖Resolve real business problems ❖ Remove unnecessary dependencies ❖ Reduce maintenance cost ❖ Prepare for changes
  • 4.
    3 Main Rolesin Architecture ❖ Data Owner ❖ Data Processor ❖ Data Consumer
  • 5.
    Everything starts fromMVC ❖ MVC: Model+View+Controller ❖ MVC in Web programming ❖ View Container: Web Browser ❖ Model: Model Layer
  • 6.
  • 7.
    The Reality ❖ MVC= Massive View Controller ❖ View Controllers have too many responsibilities: Networking, Data Access,UI configuration, Data Source, Delegate … ❖ Shared Data causes problems (Mutability Management) ❖ Controllers are the least reusable classes in the project ❖ “Cocoa MVC is the best architectural pattern in terms of the speed of the development.”
  • 8.
    Slim Model V.S.Fat Model ❖ Fat Model ❖ Business Logic in Model ❖ Slim Model ❖ Model + Helpers ❖ MVC-S: MVC+Data Store(Data Access) ❖ MVC-N: MVC+Networking
  • 9.
    The True MVCin Mobile: MVP ❖ MVP: Model+Passive View+Presenter ❖ All UI logic in presenter. ❖ View knows nothing about Model
  • 10.
    MVVM ❖ MVVM =Model+View+ViewModel ❖ ViewModel Definition
  • 11.
    MVVM:View Model ❖ Non-PatternViewModel: ViewData/Transition Data (Immutable) ❖ ViewModel in MVVM: Independent Presentation Logic
  • 12.
    MVVM: Where isthe Controller ❖ MVVM = M-VM-C-V = Model ViewModel Controller View ❖ Controller bind View and ViewModel together
  • 13.
    Benefits of ViewModel ❖No direct dependency between Model & View ❖ UI Independent Presentation Logic
  • 14.
    MVVM With Binding ❖Fine without binding ❖ Even better with binding ❖ Popular Binding Frameworks: ReactiveCocoa(ObjC & Swift), RxSwift ❖ Reactive : Learning Cost ⬆ ❖ Binding: Debugging Difficulty⬆
  • 15.
    VIPER ❖ VIPER =View+Interactor+Presenter+Entity+Router ❖ Interactor: Business Logic, using external data access layer ❖ Presenter: UI related (but UIKit independent) business logic ❖ Entities: Slim Model (Plain Data Objects) ❖ Router: Routing between different modules
  • 16.
    VIPER: Highlights ❖ Evenlydistribute responsibilities ❖ UseCase focused ❖ Able to combine different UI protocols to build up a screen
  • 17.
    VIPER: Problems ❖ Toomany classes: Development/Maintenance Cost ⬆ ❖ Too complex for simple apps: Slow down the development
  • 18.
    VIPER: Suggestions ❖ WorkloadReduce: Use Script/Template to generate all those classes ❖ Not a practical choice for s/m projects ❖ A option for large projects (Team Size ≈ 5/Development Life Cycle ≥ 6 month)
  • 19.
    Ziggurat ❖ Created bySquare ❖ Inspired by React Native & Flux
  • 20.
    Ziggurat ❖ More layersdistribute responsibility evenly ❖ The service layer encapsulates mutability ❖ Presenter & Renders are stateless ❖ One Way Data Flow ❖ Repository = Data Store ❖ View Model = View Data ❖ Drawback: Relatively hard to manage animations
  • 21.
    Rules Of Thumb ❖Dependency is the No.1 concern ❖ More layers = Less coupling = Harder to understand ❖ No right or wrong architecture ❖ Plan well then improve your architecture iteratively
  • 22.
    Massive Class Prevention ❖No more than 300 lines of code
  • 23.
    Future Topics ❖ ArchitectureDesign By Practice ❖ Protocol Oriented Design in Swift ❖ iOS Animation Design By A Live Example/Examples
  • 24.
    References ❖ http://casatwy.com/iosying-yong-jia-gou-tan-viewceng-de-zu-zhi-he-diao-yong-fang- an.html ❖ https://www.objc.io/issues/13-architecture/viper/ ❖https://www.objc.io/issues/13-architecture/mvvm/ ❖ https://medium.com/ios-os-x-development/ios-architecture-patterns- ecba4c38de52#.tlnbspmx8 ❖ https://www.raywenderlich.com/62699/reactivecocoa-tutorial-pt1 ❖ https://www.youtube.com/watch?v=4cP1p5VOrSI ❖ https://corner.squareup.com/2015/12/ziggurat-ios-app-architecture.html ❖ http://khanlou.com/2015/12/mvvm-is-not-very-good/ ❖ http://limboy.me/ios/2015/09/27/ios-mvvm-without-reactivecocoa.html

Editor's Notes

  • #4 Today’s topic is more about inner module architecture instead of High Level architecture
  • #7 Who is the “View Container” in Apple’s MVC?
  • #9 Model = Model Class Fat Slim: Both in Modal Layers
  • #10 Storyboard inits controllers: Make implementation harder Dependency Injection Presenter controls everything
  • #11 What is View Model?