SoFunction
Updated on 2025-04-10

The three most common scenarios for network request in RxJava

This article wants to explain the three most common scenarios when you develop Android applications and use RxJava as your architecture, especially regarding network requests.

I use Retrofit as the network layer, simple memory cache - HashMap for cache, and can also be replaced by Room or other database implementations.
The Retrofit interface has some simple methods as follows, which gets a list of events.

@GET("events")
Single<List<Event>> getEventsFeed(...);

Exposed through my Repository interface, you can subscribe to the following:

Single<List<Event>> source =
  (...);
source
.subscribeOn(())
.observeOn(())
.subscribe(data -> {
        // Do something with data . pass it to a view.
      },
      throwable -> {
        // Handle error . get view to show dialog.
      }
    );

In the following example, we will change the definition of "source" and the rest of the code remains unchanged. This is also one of the advantages of RxJava. It can connect complex asynchronous tasks, but the code for execution and observations can remain unchanged.

Now let’s talk about the three situations you will encounter when developing a basic or moderately complex application.
1. Obtain data from cache or network
2. Make two requests, the second request depends on the first one.
3. Send multiple requests at the same time and combine their results.

Get data from cache or network

If there is a cache, the value is taken from the cache, otherwise it is obtained from the network.

Maybe<List<Event>> source1 =
  (...);
Single<List<Event>> source2 =
  (...);
Maybe<List<Event>> source = 
    (source1, ()).firstElement();

Here we use the concat operator to connect two observables together, however firstElement means we only care about the first emitted value. So if there is a value in the cache, this value will be emitted and onCompleted is called, and this value is the return value. Network requests will not be called, which is what we expect. If the cache has no value, it will call onCompleted, and no value is emitted at this time. Therefore, the network request will occur. Using MayBe implies the possibility of unobserved values, for example, the cache is empty and the network does not return results.

Two requests are initiated, the second request depends on the first

Take values ​​from the network and use part of its results to initiate another network building request to get the data you really want.

Single<User> source1 =
  (...);
Single<List<Tweet>> source = (user -> {
    return (());
  });

Initiate multiple requests simultaneously and combine their results.

I have three network requests that are not dependent on each other and I want to execute at the same time to improve response time. The value is not transmitted until all three requests are completed.

Single<List<Event>> source1 =
  (...);
Single<List<Bookmark>> source2 =
  (...);
Single<Stats> source3 =
  (...);

Single<MyViewModel> source =
  (source1, source2, source3, MyViewModel::new);

In this example, we use the zip operator to merge the three Observables. They are combined into a MyViewModel's POJO. Their constructor parameters match the type of the source Observables

public MyViewModel(List<Event>, List<Bookmark>, Stats) {...}

When you subscribe to this source Observable, three network requests will be initiated at the same time, but the new MyViewModel instance will be emitted only after they are all completed.

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.