Chapter 4. Connecting the user interface with networking

 

In this chapter

  • Using events as a trigger for data retrieval
  • Taking a deeper look into how subscriptions work
  • Making cascading network requests

Subscriptions explained

In this chapter you’ll connect some dots and make data streams that start from the UI, trigger network requests, and end up being displayed to the user. Before you start, though, you’ll have a quick look at subscriptions in RxJava programming.

We’ve talked about subscribing, but not so much about subscriptions. In short, the subscribe function returns a Disposable object that allows us to manage the created relationship between the observable and the subscriber. Us in this context is the creator of the logic—the observables and subscribers are just building blocks at our disposal.

To see how the subscription works, you’ll explore a real-life example of a newspaper subscription. In this example, an actual newspaper is delivered to the mailbox every morning.

Subscription dynamics

After the subscription has been created, it defines a relationship between the source and the target—or the observable and subscriber. You’ve seen this before.

But, in our example isn’t Peter the subscriber?

The answer is linguistically yes, but in Rx terms, Peter only establishes the relationship between the mailbox and the newspaper company. He’s the one who defines how the two should interact.

Terminating subscriptions

RxJava 2 concepts and subscription management

Advanced Rx chains example: Flickr search client

Setting up the Flickr client project

Overview of the search chain

Step 1: A simple hardcoded search

Making it click

Coffee break

Implementing the reactive chain

How switchMap works

Getting thumbnail information

Step 1: Expand the list into an observable

Step 2: Apply operations to each item individually

Step 3: Collect results

The complete solution

Adding a username from another API

sitemap