Binding data without RxSwift

Because Rx frameworks sometimes can be a pain.

Bruno Muniz
2 min readDec 1, 2020

Back in 2017, I watched Eric Cerney at the RWDevCon talking about MVVM and data bindings. What’s cool about it, is that it does the same thing that an observable would do in Rx with much less code.

Photo by Fabian Irsara on Unsplash

Boxing

By overriding setters we can simulate observables. Skipping the MVVM refactoring, the most interesting thing here is Boxing. A technique where we wrap an object with generic type T and everytime its value gets updated a closure returns it.

This is great because it leaves our view controller completely isolated from our logic. It does exactly what a view controller should: handle views and actions. It doesn’t know what increment does, or decrement, or what’s the text value for the label. All of that logic is on the view model. This is a silly example but notice that our view controller has nothing to be tested.

With only two functions, our view model can easily be tested.

Tests

I wish It was always like this.

--

--