Codementor Events

How and why I built Speedy! A reactive Swift property observation framework.

Published Jun 12, 2018Last updated Dec 09, 2018

About me

I am a developer with going on 4 years experience in iOS and Xamarin mobile development alongside back end development (mainly AWS and .NET, but also some Swift, JS and Go) and a smattering of 3D AR / VR experience with iOS and Unity3D. Oh, and I also have some experience with ML using Tensorflow creating little neural nets.

MVC vs MVVM

Traditionally iOS (and Mac OS) development is based around MVC (Model, View, Controller). Google MVC and you'll find no end of blogs and posts about "Massive View Controller" and the downsides of the MVC paradigm. Ultimately any system is as good as it's architect but MVC does lend itself to having a lot of code in the Controller and the main issue is that it's usually both view code alongside business logic. If you're a keen TDDer this should make you shudder a little.

MVVM (Model, View, ViewModel) is an alternative that is often proposed as the solution to the issues that MVC tends to cause. Again, I've seen good MVC and bad MVVM, but experiences with both have left me squarely in the MVVM camp. Companies like Microsoft (who started the MVVM movement) have built their SDKs around this mantra whilst Apple is stuck squarely in the MVC camp. How then can we effectively harness the power of MVVM when developing apple products? Well with libraries like Speedy!

What is Speedy? A reactive Swift property observation framework.

Speedy is a "reactive Swift property observation framework". Now that's quite a buzzwordy filled sentence. It is inspired by (not based on) the ReactiveX API which is an implementation of the "Observer Pattern". The whole idea is that any interested part of an entire program or App can observe any other part. Think of the NotifcationCentre in iOS on steroids!

Why is this useful for MVVM? Well using a library like Speedy, you can group together the View and Controller into one "View" section (think along the lines of the xaml and codebehind file in Xamarin or WPF) and introduce a ViewModel. A ViewModel is an abstraction of a view's state, without knowledge of the view object itself. For example: given a label in the view, the label's text property can be represented by a single string in the view model that may be dynamically updated by the view model during the app's lifecycle.

So how does Speedy fit in? It is the glue that binds the label's text property and the string property on the view model together. The view controller "observes" the changes on the string property in the view model and updates the label's text property accordingly. Result? MVVM, separated view and business logic, powerful bindings, highly abstract-able, testable code! Yay!

Examples

Speedy is based around the Value class:

let exampleValue = Value("Hello, World!")

Value is generic and accessing the underlying value can be done with (you guessed it!) the value property:

let stringValue = exampleValue.value

Value observation can be achieved using the inspect method

exampleValue.inspect { print("Got value: \($0)") }
exampleValue.value = "Foo" // outputs: Got value: Foo
exampleValue.value = "Bar" // outputs: Got value: Bar

Values can be filtered using the when method:

exampleValue.when { $0 != "Foo" }
      .inspect { print("Got value: \($0)") }
exampleValue.value = "Foo" // outputs: Nothing
exampleValue.value = "Bar" // outputs: Got value: Bar

Values can be mapped using the map method:

exampleValue.map { "Value is \($0)" }
      .inspect { print("Got value: \($0)") }
exampleValue.value = "Foo" // outputs: Got value: Value is Foo
exampleValue.value = "Bar" // outputs: Got value: Value is Bar

So putting this all together:

class ViewModel {
  let titleText = Value("Foo")
}

class AViewController : UIViewController {
  let mv : ViewModel = ... // some DI here
    
    override func viewDidLoad() {
    	super.viewDidLoad()
        weak var weakSelf = self
        mv.titleText.inspect { weakSelf?.label?.text = $0 }
    }
}

That's all there is to it! In the above scenaro, any change at all to the view model's titleText property will be reflected in the label text in the view. Boom!

Tech stack

Speedy is written in Swift using only the Swift standard libraries which means that it can be used pretty much on any system now that Swift is open source. At the end of the day it is an implementation of a programming concept so it could be re-written in most other languages. So if you're a reactive / property observation / MVVM fan, get building!

Discover and read more posts from Rob Sanders
get started