The Rx framework has been under development for a long time and is available for other languages such as Javascript. In the past I used quite a bit of React.js and Redux so was interested in checking out RxSwift. The great thing about Rx is that once you learn it for one language, you’re pretty much set for others if you are interested in that sort of thing.

iOS Needs Reactive Programming

Having worked on quite a few large apps now, it’s become crystal clear that I want and need to have reactive programming as part of my arsenal. iOS comes packaged with quite a few ways of monitoring changes to data and updating a view as a result: KVO, Notification Centre, Delegation to name a few, but these tend to involve a lot of code to manage and, coming from a react.js background, seem highly specific to one task in particular.

Reactive programming is essentially monitoring streams of data – data that updates and changes over time. What data doesn’t update, right? How you get a view or other data model to ‘react’ to that update is a real challenge, especially when the user interface is large – trust me. Thankfully RxSwift is now very mature and able to make working with data so much more enjoyable for larger data sets inside your app. Although it currently comes in the form of a cocoapod, I believe reactive (and functional for that matter) coding is the future so it is well worth your time checking it out.

Observables

When you learn a new framework you have to learn the terms used and this can be challenging. One of the first hurdles will be ‘observables’. An observable is simply a flow of data that can be monitored or watched by something: it could be a string (a user name that changes), an Integer (a user’s currency in a game) or it could be an array of preferences. When any of these things change, these changes can be observed by something else.

Subscriptions

Think of YouTube, you want to react as quickly as possible to your favourite YouTuber when they go live, right? So you subscribe to their changes in content. Subscriptions are members inside your code that monitor changes to observables, they might update the profile image of your interface, change the points a user has, but either way they respond to changes in the data model.

Dispose Bag

To prevent memory leaks (something you wouldn’t easily get in javascript) RxSwift comes with a handy feature that when you’re not interested in that observable anymore you can unsubscribe from it’s changes but disposing of the subscription. Rather than calling ‘dispose’ all the time, you can just attach it to the dispose bag that will automatically get rid of the subscriptions for you when the memory is deallocated, sort of like ARC does with all of your other stuff for free!

So these are the main players in RxSwift, let’s look at some examples.

Let’s look at something we can observe:


1
2
3
4
// Observe an interesting String
let sequence = Observable.just("Stop observing me, it's making me anxious")
// Observe an even more interesting array
let interestingArray = Observable.from(["C# Locrian", "is an interesting", "mode in Jazz"])

We can subscribe to changes on these with the following:


1
2
3
4
5
6
7
8
9
10
11
// Observe the String from above
let subscription = sequence.subscribe { event in
   print(event)
}
// Observe the interesting Array
let subscription = interestingArray.subscribe { event in
   switch(event) {
      case "C# Locrian":
      crashAppWithWarning("Why are you talking about Jazz modes here? Oh wait, it's late on Monday...")
   }
}

Now let’s check out that dispose bag we mentioned earlier:


1
2
3
4
5
6
7
8
9
10
// Let's observe
let observyThing = Observable.from(["Merlot", "Chatauneuf", "Cabernet sauvignon"])
// Create DisposeBag
let disposeBag = disposeBag()
// Let's subscribe
let subscriptionOfWines = observyThing.subscribe { wine in
   print("You could do cool things with the UI here, or drink one of the wines above which is \(wine)"
}
// Let's make sure we dispose of the subscription after we finish
subscriptionOfWines.addDisposableTo(disposeBag)

This is a brief intro to RxSwift and hopefully you’ll prefer it over more traditional and spread out data-observing methods.