Image for post
Image for post

RxAndroid Basics: Part 2

Kurtis Nusbaum
Feb 10, 2016 · 6 min read

Welcome Back! I’m excited that you’ve decided to learn a little bit more about RxJava and how it can be used with Android. If you missed Part 1, go check it out before reading ahead. Now let’s jump into some more examples. Remember, the full set of examples in a fully functional app can be found at Also, at the beginning of each example, I’ll link to the specific Activity in which the following code snippets live.

Example 4: Subjects

Let’s make an Activity that displays a number, with a button to increment the number. Before we jump into the code though, it’s time to introduce another RxJava concept, the Subject. Subjects are special objects that are both an Observable and an Observer. I like to think of Subjects as a pipe. You can put things into one end of the Subject and it will come out the other.

There are several types of Subjects, but we’re going to use the simplest one: a PublishSubject. With a PublishSubject, as soon as you put something in one end of the pipe it immediately comes out the other.

We’re going to hook up the “out” end of the pipe first. We said that Subjects were Observables which means we can Observe them like we would any other Observable. This is how we “watch things come out of the other end of the pipe”. We set up a fairly simple Observable that just changes what our shows to the user.

Unlike our previous examples, we’re actually going to have our be called multiple times. Every time a new value is emitted, we’ll change the value of the to that new value. But how is our PublishSubject going to emit values? Let’s take a look at some code for the that we’re using in conjuction with the :

Ah ha! We see here the does two things in its callback:

  1. It increments a variable called .
  2. It calls on the with the new value of .

Since Subjects are also Observers that means they have an method. This means we can put stuff into the “in” end of the pipe by simply calling . The value will come out up in the of the Observer we subscribed in the code above. It’s like on one end of the pipe we observe the increment button being clicked and we communicate that to the Observer over on the other end of the pipe.

Example 5: Map()

Let’s make an Activity that just displays a number. This is going to be a simple Acitivity, and we’re just making it so that we can introduce a new concept: map. If you’ve ever worked with functional programming, you’re probably familiar with the map operator. You can think of map as a function that takes in one value and outputs another value. Usually there is some relationship between value put in to the map and the value that is output.

Let’s make a Single that just emits the value 4.

We want to eventually display the value our Single emits, so we need to convert it from an Integer to a String. One way we can do this is using . Like we said above, maps can take in one value and output another. This suites our purpose quite well. Since our Single will emit one Integer of value 4, we’ll use to convert it to a String, and then our Observer will take care of actually displaying it.

This is a fairly trivial use of the map function. But maps are actually quite powerful! As we’ll see in the next example, maps can be used to execute arbitrary code and help us transform data in very useful ways.

Example 6: Bringing it All Together

Let’s make an Activity that can help a user search for cities by name. In this Activity, we’re going to put everything we’ve learned together into one last mega-example. We’re also going to add a new concept: debounce. Let’s dive in.

We want to setup a PublishSubject such that it receives values the user types into a search box, fetches a list of suggestions based on that query, and then displays them. Let’s set that up:

There’s a lot here, so let’s go through things one-by-one.

The first thing you’ll notice is . What is this, and why do we need it? Well if you look at how we’ve setup the TextWatcher, you’ll notice a new value is going to come into our PublishSubject every, single time the user adds or removes a character from their search. This is neat, but we don’t want to send out a request to the server on every single keystroke. We’d like to wait a little bit for the user to stop typing (so that we’re sure we’ve got a good query) and then send our search request to the server.

This is what allows us to do. It tells to only emit the last value that came into it after nothing new has come into the for 400 milliseconds. Essentially, this means our subject won’t emit the search string until the user hasn’t changed the string for 400 milliseconds, and at the end of the 400 milliseconds it will only emit the latest search string the user entered. Perfect! This should help us avoid unnecessary searches and a UI that is constantly changing as a result of every single keystroke.

We want to use what the debounce emits to query our server via our RestClient. Since querying our RestClient is an IO operation we need to observe the emissions of debounce on the IO Scheduler. So boom, .

Cool, so now we’re emitting our search queries onto the IO Scheduler. This is where the magic of map comes in. We’re going to use map to “map” our search queries to a list of search results. Because map can run any arbitrary function, we’ll use our RestClient to transform our search query into the list of actual results we want to display.

Since our map was run on the IO Scheduler, and we want to use the results it emits to populate our views, we then need to switch back to the UI thread. So we add an . Now we’ve got the search results being emitted on the UI thread. Note the ordering of all our s here. They’re critical. We’ve essentially setup the following order of emissions:


The | represents emissions happening on the UI Thread and the ||| represents emissions happening on the IO Scheduler.

Finally, we use the results from the search to display the search results to the user. Pretty neat, right?!

Wrapping Thing Up

Well that’s it for me. I hope these examples have been helpful in teaching you some of the RxJava basics. There are of course many more aspects of RxJava that I strongly encourage you to explore. If you have any feedback for us, or any other examples you’d like to see, please drop us a line. And if you have any examples you’d like to add to our repository, please feel free to submit a pull request. Happy coding!

Further Reading

For “safer” Subjects, try RxRelays

Some details regarding Schedulers, observeOn, and subscribeOn

is actually just one of many fun operators available in RxJava.

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