Rx vs Coroutines: Complex network chains on Android

karntrehan
MindOrks
Published in
4 min readJul 27, 2019

The title of this post may trigger a debate about Rx and coroutines being completely different solutions and hence incomparable. That is completely true: Rx works with asynchronous streams of data and coroutines are (in my opinion) asynchronous callback-driven functions. Yet, they are used interchangeably on Android to drive network calls and hence this comparative post.

Photo by Ishan @seefromthesky. Banner from canva.com

I had to work with a complex network chain for my Starwars sample Android app. I ended up writing the chain using both Rx and Coroutines. This post is about my experience with both.

The chain

I was using the SWAPI service to build the following chain:

  • Hit the /people/{id/} for a particular character. This response would give you multiple /species/{id}/ urls and multiple /films/{id}/ urls.
  • Fetch the specie details from each /species/{id}/. These details would also contain a /planets/{id}/ url, hit this and get the planet details.
  • Fetch the film details from each /films/{id}/ url.
  • Combine the responses of /people/{id}/, multiple /species/{id}/, /planets/{id}/ & multiple /films/{id}/ into a model.
draw.io

Considerations

  • Some species may not have a planet. This should not break the chain.
  • Try to run the species + planets and films fetch in parallel.

As seen above, the failure of /planets/{id}/ adds some complexity to a fairly straightforward parallel chain.

The Rx Chain

Like a lot of you, Rx has been my go-to solution for asynchronous programming on Android for some time and is the first solution I tried to run this complex chain.

1. Get basic details

2(a). Get species & planets details from basic details

2(b). Get film details from basic details

3. Merge species, planets and film details to basic details and send to UI

full code here

The Coroutines Chain

This is my first complex experiment with Kotlin coroutines and may not be optimum. Coming from an Rx world, it was a bit tricky to get the coroutines to run in parallel and handle the failure of /planets/{id}/ at first.

1. Get basic details

2(a). Get species & planets details from basic details

2(b). Get film details from basic details

3. Merge species, planets and film details to basic details and send to UI

full code here

Observations

Both Rx and Coroutines worked well for the experiment. Following are my observations:

  • Performance: When implemented correctly, both Rx and Coroutines have a similar performance and the user sees no visible difference. We will look into memory usage by both in more detail in the coming parts.
  • Parallelism: Both Rx and Coroutines ran in parallel. The boilerplate to make this possible was fairly similar.
  • Error handling: Both handled the failure of /planets/{id}/ api fairly easily, but I personally like Rx’s onError() handling compared to a try/catch block around each call.
  • Testing: We will look into this in more detail in the coming parts.

Source

All code images created with https://carbon.now.sh

--

--