RxSwift Made Easy : Part 1 — Primer

Tim Beals 🎸
Swift2Go
Published in
4 min readAug 19, 2018

Welcome to my first article in a series on RxSwift. If you have had any interest in learning about reactive approaches and bringing them to your iOS projects, you’re in luck! This series will progress through increasingly complex features and illustrate their real world application in code. Let’s get started!

What is RxSwift?

Reactive Programming has been around for almost 20 years, but has really only gathered attention in the last decade. In order for Reactive Programming to be used across multiple platforms, extensions have been developed across the most popular programming languages. The prefix Rx (as seen in RxSwift) means “Reactive Extension”. The important thing to know about extensions is that the underlying approach is exactly the same for all languages. This is cool for many reasons, but three that immediately come to mind is that:

  1. You can have a reactive programming approach using your native platform and language.
  2. You can easily collaborate across languages and platforms.
  3. Once you learn reactive programming in one language, you can directly transfer that knowledge to any other language that has a Reactive Extension.

Sounds promising. But what is it?

Reactive programming is an approach that deals with asynchronous sequences of data. That is, observable sequences of elements that emit events to observers.

It can be a little intimidating to get started with reactive programming, so in the next section, we will layout the stages for creating and observing a sequence and then explain important terms and concepts for each stage.

Getting started with your first observable sequence

Let’s setup a basic observable sequence and then go through an explanation of each step:

Explanation

  1. Create a sequence of elements (line 3)

In Reactive programming you use operators, but you should really think of them as another name for methods. Here, we are creating a sequence of type Observable<String> using the .of operator. As you can probably guess, this means that we have four string elements contained in our sequence, and this sequence is observable; in other words, objects can subscribe to receive events from the sequence.

The .of operator allows you to create the sequence from an inferred variadic type. You could also use: .just which creates a single element sequence, or .from which creates a sequence from an array

2. Create a dispose bag (line 5)

In a moment we will discuss subscriptions in some detail but first, it’s important to know that when an object subscribes to a sequence, it isn’t directly holding reference to it. Therefore, if you don’t manually terminate your subscriptions, you can risk leaking memory. It is possible to dispose of your subscriptions individually, but to make life simple it is most common to create an object of type DisposeBag. Essentially, the dispose bag holds any number of objects that conform to the Disposable protocol. In its deinit method, the dispose bag goes through each of the disposable objects and removes them from memory.

3. Subscribe to the sequence (line 7–9)

When you use the .subscribe method, you are subscribing an event handler to an observable sequence. In the closure, you specify how you want to handle the different events that are emitted by the sequence. In this case, regardless of the event type, we are printing the event to the console. You can see the output is:

next(One), next(Two), next(Three), next(Four), completed

Under the hood, Event is simply an enum with three cases:

next(Element) — when the sequence iterates over an element it will send the next event, with the element as an associated value

error(Swift.Error) — when the sequence encounters an error, it can send the error event with the error type as an associated value and terminate the sequence.

completed — When the sequence has finished iterating over every element normally, it will emit the completed event.

4. Dispose of the subscription (line 11)

In addition to handling the events of a sequence, the .subscribe operator returns a Disposable object that can be added to the (you guessed it) dispose bag. The .diposed(by: DisposeBag) operator, simply adds the subscription (of Disposable type) to the dispose bag. When the end of this code block is reached, the deinit method described in step 2 is called.

Further Simplification

…and there you have it! As you can see, in very few lines you can set up an observable sequence, subscribe to that sequence with an event handler, and dispose of the subscription when you are done. Before concluding this post, it is worth noting that the syntax can be simplified from the above example as many of the operators return a type that allows you to chain your next operator. This is what the above example looks like with chained operators:

Now that you have this introduction under your belt, check out Part 2: Working with Subjects

— Tim Beals www.roobicreative.com

--

--