iOS Developer Meetup. Allan Barbato, iOS ... High level structures of a Software system. Science of creating and ... Arc
Architectures & Swift iOS Developer Meetup
Allan Barbato, iOS Developer
You said Architecture?
High level structures of a Software system
Science of creating and documenting such structures
Elements, relations among them & their properties
Why care about software Architecture?
Organization Consistency Maintainability Debugging From this:
To that:
Real world Architecture: iOS
MVC (Model View Controller)
MVVM (Model View View-Model)
VIPER (View Interactor Presenter Entity Router)
You are already following an architecture!
MVC: Definition
Model Data representation, local & remote iOS: CoreData, Realm, simple struct/class
View Layout iOS: UIView
Controller User input, view life cycle, ... iOS: UIViewController
MVC
What it is:
Apple Expectation:
Apple Reality:
MVC: Show me dat code /// Controller
/// Model
class MyViewController: UIViewController
struct User
{
{ var name: String
/// View @IBOutlet weak var textLabel: UILabel! /// Model var user: User? override func viewDidLoad() { super.viewDidLoad() // Request API for user information getJSON("/path/to/api", completion: { json in // Assign user if let name = json["name"] as? String { self.user = User(name: name) } else { self.user = nil } // Update view self.textLabel.text = self.user?.name }) } }
}
MVC: So what? Problem:
Possible Solutions: Don’t put all your code into Controllers
Controller controls everything ●
View life cycle
●
User inputs
●
Data requests
●
Data transformation
●
You name it…
Delegate logic to handlers ●
NetworkManager
●
APIManager
●
LocationHandler
●
...
Put some logic into the model Massive View Controller
Get isolated, testable chunks of code
Unit tests? Change your architecture
MVVM: Definition
Model Data representation, local & remote. Data logic? iOS: CoreData, Realm, simple struct/class
View Layout, user input, view life cycle iOS: UIView + UIViewController
View-Model Network, Business & Persistence logic iOS: struct or class.
MVVM: Show me dat code protocol MyViewModelDelegate
class MyViewModel
{
{ func userDidChange(user: User?)
/// Model
}
var user: User?
/// Controller
var delegate: MyViewModelDelegate?
class MyViewController: UIViewController, MyViewModelDelegate {
func getUser() /// View
{
@IBOutlet weak var textLabel: UILabel!
// Request API for user information getJSON("/path/to/api", completion: { json in
/// View-Model let viewModel = MyViewModel()
// Assign user if let name = json["name"] as? String {
override func viewDidLoad()
self.user = User(name: name)
{
} else { super.viewDidLoad()
self.user = nil }
viewModel.delegate = self // Notify view // Request View-Model for user information
delegate?.userDidChange(self.user)
viewModel.getUser()
})
}
} }
func userDidChange(user: User?) { // Update view element self.textLabel.text = user?.name } }
MVVM: View-Model Role
Middle man Abstract logic away from VC ●
Network
●
Business
●
Persistence (local storage)
UIKit independent representation of the View and its state Uses Bindings to communicate with the View ●
Protocol
●
KVO based binding libraries (RZDataBinding, SwiftBond, Custom imp.)
●
Full scale FRP beasts (ReactiveCocoa, RxSwift)
MVVM: Pros & Cons Pros Well defined roles Lighter View / View Controller Code far more testable Consistency between views
Cons Massive View View-Model Where to put what? Similar operation in different Views More code than MVC
VIPER: Definition
View Layout, user input, view life cycle
Interactor Business logic related to the data. Network & Persistence
Presenter UIKit independent representation of the View and its state
Entity Plain data object. Logic is handled by Interactor
Router Interaction between VIPER modules AKA screens
VIPER
Conclusion
●
MVC is fine until it’s not
●
Choosing an architecture is hard
●
Maintaining it is harder
●
There are no silver bullet
●
Keep things as simple as possible, not simpler (avoid overkill)
Small size app:
MVC
(prototype, MVP)
Middle size app: MVC + MVVM (most of apps on AppStore, Tinder, Slack, ...)
Huge size app:
VIPER
(Facebook, Google Maps, ...)
Conclusion
Organisation Consistency Maintainability Debugging
Thanks! Questions ?