Coroutines Simplified

Sachin Kumar
4 min readApr 26, 2020

--

In this article, we are going to understand what exactly the coroutines in Kotlin are and how we can use it to fetch the data from the backend in our android app.

What is Coroutines?

In Coroutines: Co means cooperation and routines mean functions, which is simply the cooperation between the functions.

It is an optimized framework written over the current threading framework to support multithreading operations. It is a lightweight thread and more powerful than the actual thread.

Threads are managed by OS whereas coroutines are managed by the developers.

To get more about the coroutines you can follow the following article by MindOrks:

To apply this concept, here we are going to implement the basic sample app, in which we will fetch one “Hello, Coroutines!” message from the backend API by launching Coroutines and display it on the home screen of the app.

We are also going to use the MVVM design pattern for the same, in which ViewModel will handle the logic to launch the coroutines.

We have divided our project into the following packages:

  • model: This contains our POJO class and retrofit implementation to fetch the message from the backend.
  • utils: This contains enum and various status flag handlings like SUCCESS, ERROR, and LOADING.
  • viewmodel: This contains our ViewModel which consists of the business logic for launching the coroutines.
  • MainActivity: This is our view which holds textview to display the fetched message.

Now, first, let’s start with adding retrofit and ViewModel related dependencies in our build.gradle(:app).

To implement retrofit in android, please follow the following article:

In the above article, please follow till Step no. 5 because here we are not going to use RxJava as well as the callback which retrofit provides.

Reason:

Starting from Retrofit 2.6.0 you no longer require the Call Adapter as Retrofit now includes built-in support for Kotlin suspend modifier on functions.

So, ApiInterface will be like:

Here, we have added suspend before the getMessages() func for retrofit implement and the callback part will be handled by retrofit internally, we will simply call this func from ViewModel via ApiHelperImpl class and the response will be provided in Coroutines launch scope in ViewModel itself.

Let’s come to the interesting part i.e. our ViewModel where we have added the following func which requests for the message from backend API using Coroutines, here we are launching the Coroutines using viewModelScope.launch{} and it has the scope till ViewModel’s onCleared() gets called. For requesting a message, we are calling suspend func from the launch block which is pointed using Green color.

But before requesting for the message we thought to display the loading progress which is pointed using Blue color, for that we are calling postValue() of our MutableLiveData so that we can display the progress by sending the callback to our observer in our MainActivity.

Note: When suspend func gets started or called, it will pause the subsequent code execution until it manages to fetch the message or response from the backend.

If it manages to get the response successfully it will execute the code present in the next line and post the result back to MainActivity and loading progress will be dismissed. But, it might be the case it gets failed then the exception/error will be handled in the catch block and will post it back to MainActivity so that loading progress will be dismissed like a success case.

Our MainActivity’s observer is like:

As we can see above MainActivity is our View so it handles the logic to update UI part of our screen.

It was the basic implement of Kotlin Coroutines using retrofit and we have seen how we can make our synchronous code to act like asynchronous without actually writing asynchronous code. In the past Async as well as RxJava have been our favorite but now, if we are using Kotlin then it’s time to switch to Coroutines.

For accessing this implementation, kindly follow our GitHub link:

Thanks, for reading our article and if you have liked it, please hit the clap button :).

--

--

Sachin Kumar

Senior Java Backend Dev | Expertise in Java Microservices, Spring Boot Framework & Android apps development.