Although ReactiveCocoa looked promising, it had a few things that are not yet there. These are outlined in an earlier article, ReactiveCocoa and MVVM Initial Experience. So, it was time to explore RxSwift.
I started with reading the concepts in the README. It was pretty straight forward. My next move was to find an example which is like the ReactiveCocoa oriented ReactiveTwitterSearch example that I loved so much. Where to begin? Good thing I discovered the RxSwift slack community.
Having an active slack community is a huge win! With the help of Carlos García in slack, I found GitHubSignup in the RxExample examples. This was definitely what I was looking for.
For fun, I created a small demo app which explored the bindings. I decided to see what was there for a UIPickerView and UITextView.
Compared to ReactiveCocoa, the RxSwift repo comes with extensions to some of the UIKit components. These are known as RxCocoa extensions. RxCocoa is located alongside the RxSwift code in the GitHub repository. However, if you are using CocoaPods like I am, RxSwift and RxCocoa are two separate pods.
After much searching, I discovered there is no extension made for the UIPickerView. So, I just made a PickerViewAdapter which contains a selectedPerson RxSwift Variable and PickingPersonViewModel. The adapter handles the UIPickerViewDelegate and UIPickerViewDataSource while calling through to the PickingPersonViewModel. The adapter is used by the View Controller.
With the UITextView, I had much better luck. With a simple binding, I was able to hook up a notes UITextView up with a specific note in the NotesViewModel. Changes to the UITextView would be reflected in the note. Pretty neat! Code is here:
_ = notesTextView.rx_text.subscribeNext { someText in
// Changes to notesTextView’s text triggers this block.
self.notesViewModel.currentNote().value.text = someText
}
How do extensions like rx_text work? I looked at the simplest example, the RxSwift UILabel extension. AnyObserver has an observer which is an event handler. As can be seen in the code, the UILabel extension with rx_text is an adapter that handles String events.
Although it’s currently Beta, there’s a sense that RxSwift will be out of beta soon. This is based on a Github request for it to be released as 2.0.0.
Based upon my observations and discussing it with some smart people at CARFAX I know, it looks like RxSwift is pretty solid. If you are interested in supporting MVVM in a Functional Reactive programming style, I recommend you check it out!