36

HAs anyone implemented MVVM pattern in your iOS app without using ReactiveCocoa?

Found lots of examples here, but all of them use Reactive Cocoa. I wanted a simple example of MVVM implementation.

Smitha
  • 6,110
  • 24
  • 90
  • 161
  • 2
    Your question is interesting but too broad. What do you have in mind? Otherwise, you should take a look at these two blog posts: [Swift: Using MVVM To Work With Optionals](http://natashatherobot.com/swift-mvvm-optionals/) and [Introduction to MVVM](http://www.objc.io/issue-13/mvvm.html). You may also read this thread: [Rewrite code from Objective-C to conform with Swift power tools and concise style](http://codereview.stackexchange.com/questions/62958/rewrite-code-from-objective-c-to-conform-with-swift-power-tools-and-concise-styl). The excellent answer given by Rob Mayoff uses MVVM setup. – Imanou Petit Oct 03 '14 at 23:57
  • 2
    Another Swift MVVM example on GitHub [here](https://github.com/shilgapira/SwiftDemoMVVM). – Imanou Petit Oct 04 '14 at 13:41
  • You'll only get benefits of adopting MVVM if you have a system that helps you with data-binding. In iOS this is almost certainly going to be KVO mechanisms. You'll need this to avoid writing your own change tracking code for every property. ReactiveCocoa is the most mature project that unifies the KVO API together with notifications and asynchronous streams and is incredibly powerful once you get over the learning curve. I would advise not trying to avoid it, but actually to embrace it. – fatuhoku Nov 15 '14 at 09:22
  • https://www.captechconsulting.com/blogs/ios-design-patterns-mvc-and-mvvm – Sachin Kumaram Mar 04 '16 at 13:24
  • Take a look here: https://www.mobiledefense.com/blog/2016/02/07/pure-swift-mvvm/ – nmdias Dec 11 '16 at 09:30
  • In this post, there is a nice explication on how to use MVVM on iOS without any binding framework. https://tech.olx.com/clean-architecture-and-mvvm-on-ios-c9d167d9f5b3 – Oleh Kudinov Aug 22 '19 at 11:38

1 Answers1

45

Reactive Cocoa is definitely not required for MVVM. I have built a very successful MVVM framework w/o any bindings at all. Bindings are not a requirement for MVVM.

Specifically, the linkage between the View Model and the View does require the View Model to signal to the View that it needs to update its data. This can be achieved using Reactive Cocoa, KVO (I really like Facebook's KVOController), or even using a simple delegate pattern.

The View Model knows when the View needs to update - either data has changed, or you're making an async data request via the Model and the Model has been loaded into the View Model.

When you set up your View, you could bind each control to the corresponding value on the View Model. I have found that when I need to churn out screens, this can get very tedious. Instead, I'd rather have a single method that is called when the View Model signals that the View should update itself. Within that method, I'm simply going to set all of my control properties.

Now, you only need to concern yourself with how that method is triggered. In my personal framework, I leverage KVO and my ViewControllers monitor a timestamp property on my ViewModel baseclass. Any time my view models update their underlying data, its timestamp is updated which triggers the update. You could just as easily have the View Controller register itself as the ViewModel's update delegate and use a standard delegate pattern.

Again, MVVM is not about specific implementation requirements, and more about a higher level concept of separation of concerns, dependency decoupling, and encapsulation.

Drew Beaupre
  • 2,437
  • 1
  • 17
  • 17