RxSwift: Reactive Extensions for Swift Part 1

Tiago Martinho
Jan 29, 2017 · 3 min read
Image for post
Image for post

In this first part of this blogpost we are going to explore the theory in order to understand RxSwift, specifically ReactiveX. In the second part we are going to apply this concept to create a sample iOS application using RxSwift.


“ReactiveX is a combination of the best ideas from the Observer pattern, the Iterator pattern, and functional programming”


The Observer and Iterator patterns, described in the GoF book, are two patterns used in a Producer/Consumer scenario.

The Iterator Pattern is used to pull data, typically from a Collection of Objects, as long as it implements the iterable interface. This is a synchronous and interactive, I’m reading the current value and asking for the next one till I’m satisfied or the collection ends.

The Observer Pattern is used to Push data, objects that are interested in the data subscribe to receive data when it’s available. This is asynchronous and reactive, when new data is available to the subscribers they are notified with the data.

What ReactiveX explores is the correspondence between this two patterns, and unifies the semantics between them (Observer is Dual to Iterator).


“Rx is that sweet spot between functional and imperative worlds. It enables you to use immutable definitions and pure functions to process snapshots of mutable state in a reliable composable way.”

Rx defines Observables that are a sequences of elements that enable easy composition of asynchronous operations and event/data streams.

Rx allows us to create event/data streams and to compose and transform them with operators. There are operators like filter, map, combine, retry, observe, bindTo, zip, throttle. The list of all the operators is available here: Operators List.

You can also subscribe to any observable stream to perform side effects.

Rx works well with a stateless architecture, data doesn’t need to be actually stored anywhere, it can pass from one system to another applying different transformations from pure functions, a stream transformation into another stream.


In this example we are making a search on GitHub, this is an example taken from RxSwift.

We create an observable on the main thread from the searchBar text. This stream is throttled in order to avoid making a request every time the text changes. The stream only send events if the text actually changes. We always receive the latest text entered by the user and if it’s not empty we make a search request.

We can bind this search results to a table view in order to display the results we get when the user updates the search.

Here we can see it working:

Image for post
Image for post


In here you can find some resources if you want to get started with RxSwift.

The Reactive Manifesto

ReactiveX Project

ReactiveX Tutorials

RxSwift Project

Interactive diagrams of Rx Observables

Expert to Expert: Brian Beckman and Erik Meijer – Inside the .NET Reactive Framework (Rx)

Reactive Programming Overview (Jafar Husain from Netflix)

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store