---
title: "#163: Creating Observables with RxSwift ⚗🛠"
url: https://littlebitesofcocoa.com/163-creating-observables-with-rxswift
author: Jake Marsh
date: 2016-01-06
published: 2016-01-06
topics: ["Techniques","Libraries"]
description: "In Bite #162, we took our first look at RxSwift, and how it can transform how we write code. We learned how Observables can simplify our code, and mentioned..."
---

# #163: Creating Observables with RxSwift ⚗🛠

By Jake Marsh, January 6, 2016. HTML: https://littlebitesofcocoa.com/163-creating-observables-with-rxswift

In [Bite #162](https://littlebitesofcocoa.com/162), we took our first look at [RxSwift](http://git.io/rxs), and how it can transform how we write code. We learned how _Observables_ can simplify our code, and mentioned creating our own _Observables_ briefly, but today we'll dive a bit deeper. Let's begin:

Creating our own _Observables_ is how we can bring code that wasn't built with reactive principals in mind, into the [RxSwift](http://git.io/rxs) world. It's helpful to think of _Observables_ as merely a slightly different way to organize code we were already writing.

Let's look at a simple asynchronous **function** call:

``` swift
let task = loadSpaceships { spaceships, error in
  guard error == nil else { handleError(error); return }
  self.resultsTableView.reloadData()
}
```

We can actually use this code as a starting point to write a new  **function** that wraps this code and converts it into an _Observable_.

We'll be responsible for <i>sending values</i> along, reporting any <i>errors</i>, and letting **observers** know if/when the work is <i>completed</i>.

``` swift
func spaceships() -> Observable<[Spaceship]> {
  return Observable.create { o in
    let task = loadSpaceships { spaceships, error in
      guard error == nil else { o.onError(error!); return }

      o.onNext(spaceships)
      o.onCompleted()
    }
    
    return AnonymousDisposable { task.cancel() }
  }
}
```

We use _Observable's_ `create` **function**  to get a reference to an **observer** in a **closure**. We can use this **observer** to send along relevant status updates.

Lastly, we return an **AnonymousDisposable** closure that cancels the **NSURLSessionDataTask** when we're disposed.

Now this code can live in the [RxSwift](http://git.io/rxs) world as an _Observable_, be subscribed to, and be used alongside any other _Observables_:

``` swift
Observable.zip(crew(), spaceships()) { return ($0, $1) }
  .subscribeNext { print($0) }
```

One last note: We've been talking about all of this in terms of [RxSwift](http://git.io/rxs), but other reactive or functional libraries might have different names for their streams of values. Even if we're using something other than [RxSwift](http://git.io/rxs), the underlying concepts will usually remain largely the same. We can still use the techniques here to _lift_ the code out of the conventional world and into the reactive one, regardless of which library we're using.
