Which architecture
should I use?
Esteban Torres - @esttorhe, MobOS, 2017 2
Slide 3
Slide 3 text
What is the
«Problem»?
Esteban Torres - @esttorhe, MobOS, 2017 3
Slide 4
Slide 4 text
Not only this but we also lack certain «pieces»
along this architectures
or lack a clear understanding of where things
belong
Esteban Torres - @esttorhe, MobOS, 2017 4
Slide 5
Slide 5 text
What DO we have ?
Esteban Torres - @esttorhe, MobOS, 2017 5
Slide 6
Slide 6 text
What DO we have?
(some of them at least)
Esteban Torres - @esttorhe, MobOS, 2017 6
Slide 7
Slide 7 text
MVC
Esteban Torres - @esttorhe, MobOS, 2017 7
Slide 8
Slide 8 text
Esteban Torres - @esttorhe, MobOS, 2017 8
Slide 9
Slide 9 text
The «Problem» of MVC
Esteban Torres - @esttorhe, MobOS, 2017 9
Slide 10
Slide 10 text
MVC
Verdict: By its basic definition not good enough
for complex apps
Esteban Torres - @esttorhe, MobOS, 2017 10
class ViewModelA {
private let model: Model
var formattedCurrency: String…
var formattedProperty1: Double…
var formattedProperty2: String…
…
…
init(withModel model: Model) …
func getModels(completion:([Model]) -> ()) -> Void {
API.request(.GetModels) { jsonResponse in
// Parse the JSON response here
let models = …
completion(models)
}
}
}
Esteban Torres - @esttorhe, MobOS, 2017 37
Slide 38
Slide 38 text
!
Esteban Torres - @esttorhe, MobOS, 2017 38
Slide 39
Slide 39 text
MVVM
…an abstraction of the view exposing public
properties and commands… In the view
model, the binder mediates communication
between the view and the data binder.
The view model has been described as a state
of the data in the model.1
1 http://wayback.archive.org/web/20080201101909/http://
www.acceptedeclectic.com/2008/01/model-view-viewmodel-pattern-for-
wpf.html
Esteban Torres - @esttorhe, MobOS, 2017 39
It worked
Esteban Torres - @esttorhe, MobOS, 2017 42
Slide 43
Slide 43 text
Esteban Torres - @esttorhe, MobOS, 2017 43
Slide 44
Slide 44 text
class ViewModel {
// Hold a reference to our DataController
private let dataController: DataController
// Hold a reference to the model we will be getting
private var model: Model?
func getModel(id: Int) -> Observable {
return Observable.create { observer in
dataController.getModel(id)
.subscribe(onNext: { [unowned self] model in
self.model = model
// If we need to format something else we should do it here
observer.onNext(self.model)
}, onError: { error in
// Maybe properly format the error to the user here as well
observer.onError(error)
}
).addDisposableTo(disposeBag)
}
}
}
Esteban Torres - @esttorhe, MobOS, 2017 44
Slide 45
Slide 45 text
class ViewController: UIViewController {
// Retain a reference to our ViewModel
private let viewModel: ViewModel
func loadData() -> Void {
self.viewModel.loadModel()
.subscribeOn(MainScheduler.instance)
.subscribe(onNext: { [unonwed self] viewModel in
// Update your `UI` accordingly
self.nameLabel.text = viewModel.formattedName
self.amountLabel.tet = viewModel.hopsAmount
}, onError: { error in … }
})
.addDisposableTo(disposeBag)
}
}
Esteban Torres - @esttorhe, MobOS, 2017 45
Slide 46
Slide 46 text
So… what did we do? !
Esteban Torres - @esttorhe, MobOS, 2017 46
«That was the worst idea EVER…
BUT seeing how badly they tackled that
problem gave me an idea about how to
actually make it work.»
Esteban Torres - @esttorhe, MobOS, 2017 48