The long road to Coroutine from Callback in Android

Ted Park
TedPark Developer
Published in
3 min readSep 26, 2021

As Android Developer, We always consider about async task
(API Networking, observe location, query database, …)

I introduce some history for handle async task with sample code
Step by Step

How are you handling asynchronous operation requirements now?

Requirements

  1. Get user info by name
  2. If user’s job is ‘developer’, then salary is $100,000 else $0.1
  3. Notify to user salary is changed

Stage 1: Callback

Callbacks have been around since Android was born.

"oh yeah I’ll tell you when you’ve done what you’re supposed to do~

Callback is a concept in any language,
It’s been there from scratch on Android too, so it’s familiar to all developers.

However, everyone goes through hell when using Callbacks.

Aka callback hell

If the linked asynchronous operation is continued continuously,
As the callbacks continue to be nested, the indent continues to increase.
You will meet the { { { { { { { hell
(Who am I? Where is it? …)

Stage 2: High Order Function

Another problem with the callback pattern is that you have to create a listener and use that listener to know the completion of an asynchronous operation.

If you are using Kotlin, use High Order Function.
You can process to get the result directly without creating a listener class.

However, the nesting problem, which is still the problem of callback hell, has not been solved.🤦🏻‍♂️

Stage 3: RxJava

The concept of Rx is a library of concepts that not only Android developers but also iOS and Web developers know and use a lot.

It can be seen as a concept of the Promise pattern introduced in JavaScript ES6.

By using RxJava, you are freed from callback hell.
It can be executed sequentially while chaining multiple function.

Stage 4: Coroutine

I got out of callback hell with RxJava, but I’ve been living with it for a while now that I can’t shake the feeling that I don’t like something.

In the meantime,
looking at the Async/Await pattern introduced in JavaScript ES7,
I was so envious of this concept.(Add it to Android too!!)
So we met Coroutine.

Asynchronous functions in coroutines can be written just like synchronous functions. (this is true)

As if writing down the content that was given to me
You can use the function intuitively.

how is it? That’s easy, right?

The history of handling asynchronous tasks performed in Async so far
I went through it step by step in a very simple way.

For an 11-year-old Android developer like me,
I am reminded of many experiences that have passed through the above process.

I have only compared and introduced tasks for very simple requirements.

Of course, the reasons for using RxJava and Coroutine are not the only reasons mentioned above.
More complex and difficult tasks can be expressed in efficient and intuitive code using RxJava or Coroutine.

You can even use Coroutine to replace RxJava.
(Flow, Channel, …)

My conclusion is that coroutines are the best! Coroutine!

thank you.

--

--