Chapter 5. Advanced RxJava

 

In this chapter

  • Creating your own observables
  • Threading with RxJava
  • Understanding subjects and what they’re good for
  • Cleaning up subscriptions

Observables and subjects in detail

You’ve seen observables in many forms, but in this chapter you’ll learn how to create your own. Seeing the internals can help you to understand how the system works, even if usually this isn’t necessary for the applications you’ll build.

The second important topic we’ll discuss is threading. You’ve already switched the execution back to the main thread in order to update the UI, but you never quite saw how it works. This time, you’ll see what RxJava provides to freely manipulate the thread of execution, starting from the internal logic of the observable itself.

We’ll also cover a new matter called subjects. A subject is a fusion of an observable and a subscriber, combining the best and the worst of both. You need to know how to use subjects, though it’s perhaps even more necessary to understand why most of the time you shouldn’t use them.

The preceding image is a simple abstract to help you get an idea of what I’m talking about, and we’ll get into the details later in the chapter. You’ll also see the valid use cases for subjects.

Example: File browser

User flow of the File Browser app

Getting the file listing for a directory

Threading basics

Threading in functional programming

Changing the thread by using getFileListingObservable

Making the file listing dynamic

Making the list click

Different types of subjects

Using a subject as the FileObservable

Coffee break

Adding buttons for Previous and Root

Expanded graph for Previous and Root

Improved version with cleaner observables

The Previous button

Putting it all together

The detailed graph

The full code so far

Saving and releasing the subscriptions

sitemap