title: MVVM generic networking architecture
body: <p>I develop iOS applications using the Model View ViewModel paradigm to structure my view controllers and to represent their data. That in conjunction with <a href="https://github.com/ReactiveCocoa/ReactiveCocoa" rel="noreferrer">ReactiveCocoa</a> is a powerful tool; view controllers become less bloated, view models are easier to test, and there's a clear separation of concerns.</p>
  
  <p>The one problem I have with this particular architecture is that like MVC, there's still not a clear place or way to structure networking code.
  Take the following trivial example:</p>
  
  <pre><code>class HomepageViewModel {
      var posts: MutableProperty&lt;[Post]&gt; = MutableProperty([])
  
      func fetchPosts() -&gt; SignalProducer&lt;[Post], NSError&gt; {
          return SignalProducer { observer, disposable in
              // do some networking stuff
              let posts = ....
              observer.sendNext(posts)
              observer.sendCompleted()
          }
      }
  }
  </code></pre>
  
  <p>Then in my view controller somewhere I can do:</p>
  
  <pre><code>self.viewModel.posts &lt;~ self.viewModel.fetchPosts().on(next: { _ in self.collectionView.reloadData() })
  </code></pre>
  
  <p>To me it feels like the whole point of using MVVM was to not expose the views and view controller (what I call the view presentation layer) to any networking code whatsoever but I still need a way to be able to observe that new content has been fetched without knowing the specifics, just that a successful fetch occurred. I'd imagine that would look something like:</p>
  
  <pre><code>self.viewModel.contentUpdatedSignal.observeNext { _ in self.collectionView.reloadData() }
  </code></pre>
  
  <p>At the same time, I also don't want to lose the ability to bind signals and signal producers to mutable properties on my using <code>&lt;~</code>.</p>
  
  <pre><code>class ViewModel {
      let someProperty = MutableProperty&lt;[SomeModel]&gt;([])
      var (contentUpdatedSignal, observer) = Signal.pipe()
      init() {
          self.someProperty &lt;~ self.fetchContent().on(next: { _ in observer.sendNext() }
      }
  
      func fetchContent() -&gt; SignalProducer&lt;[SomeModel], NSError&gt; {
          // do some fun stuff
      }
  }
  </code></pre>
  
  <p>This method of doing this is a little better but it still uses side effects to send a next event on the signal observer and if you're using a common <code>ViewModel</code> base class, you have to expose that observer so that subclasses can use it.</p>
  
  <p>I'm looking for any improvements that can be made to the MVVM architecture, either changes to the architecture itself so that it's no longer MVVM and facilitates networking in a better and more generic way or even some sort of generic base protocol for view models that abstracts the whole process away.</p>
  
  <p>For me, being generic as possible while exposing as little info about the view model as possible is key. Ideally, I'd like to have each view controller interact with the view model in exactly the same way in regards to how networking works.</p>
  
  <p>EDIT:</p>
  
  <p>Per at @lonut's suggestion, I moved some of the networking code to my model classes but only as static methods:</p>
  
  <pre><code>import Foundation
  import ReactiveCocoa
  import Moya
  
  protocol RESTModel {
      typealias Model
  
      static func create(parameters: [NSObject: AnyObject]?) -&gt; SignalProducer&lt;Model, Moya.Error&gt;
      static func find()  -&gt; SignalProducer&lt;Model, Moya.Error&gt;
      static func get(id: String) -&gt; SignalProducer&lt;Model, Moya.Error&gt;
      static func update(id: String) -&gt; SignalProducer&lt;Model, Moya.Error&gt;
      static func remove(id: String) -&gt; SignalProducer&lt;Model, Moya.Error&gt;
  
  }
  
  extension RESTModel {
      static func create(parameters: [NSObject: AnyObject]? = nil) -&gt; SignalProducer&lt;Self.Model, Moya.Error&gt; {
          return SignalProducer.empty
      }
      static func find() -&gt; SignalProducer&lt;Self.Model, Moya.Error&gt; {
          return SignalProducer.empty
      }
      static func get(id: String) -&gt; SignalProducer&lt;Self.Model, Moya.Error&gt; {
          return SignalProducer.empty
      }
      static func update(id: String) -&gt; SignalProducer&lt;Self.Model, Moya.Error&gt; {
          return SignalProducer.empty
      }
      static func remove(id: String) -&gt; SignalProducer&lt;Self.Model, Moya.Error&gt; {
          return SignalProducer.empty
      }
  }
  </code></pre>
  
  <p>This way models can implement network calls at will which has the benefit of abstracting away the implementation details such as specific Moya network calls, mapping of response objects, etc.</p>
  
  <p>Imagine you have a <code>User</code> model:</p>
  
  <pre><code>User.get("myUserID")
  </code></pre>
  
  <p>It doesn't entirely solve the problem of how the view controller and the view model should interact with each other but it definitely moves the network code to a single point of failure.</p>