dimanche 4 juin 2017

MVVM (or even MVC) and Reactive Programming: What to do with Data Models?

Using Reactive Programming (for my purpose, I'm speaking of RxSwift in Swift 3.x), I'm having a little trouble reconciling what to do with data models that would persist (hopefully) throughout the lifecycle of an application using MVVM as an application design pattern.

Given the Reactive Programming works on things like Observables with a notion of Variables (which in and of themselves are observable properties/variables), where do persistent data models fit into the MVVM pattern.

My current understanding of the situation, we setup a Data Model (let's say User, Dog, Car). The user model is easy, because we'll make it a singleton that can be reached with something like

User.current

But then we load of list of Dogs and a list of Cars. We create some view models

class DogListViewModel: SomeProtocol {}

And then a view (and controller)

class DogListView: UIView {
    let dogs: Variable<[Dog]> = Variable<[Dog]>([])
}
class DogListViewController: UIViewController {
    var canvas: DogListView { return self.view as! DogListView }
}

My 2 questions:

  1. In this setup (given that we're using RxSwift or a Reactive Library of another language as needed), how do we persist these models so various view models can get ahold of them as they're (the view models) are instantiated and deallocated without having to constantly fetch the data models again (say, from a server)?
  2. Should observables be used in any layer, or using MVVM(C) should Observables be confined to ViewModels and Variables to Models (which seems to make sense in my head, but perhaps it's wrong)?

I think I provided enough info to get a generic question, but this may become a philosophical argument, so if it becomes that way, I may update this with a more exacting example.

Aucun commentaire:

Enregistrer un commentaire