Using Coroutines without LiveData And LifeCycle Events

Lokesh Deshmukh
Prog-Ramming Solutions
2 min readJul 18, 2020

So, I was going through a lot of articles and they just confused me. My question to the google was:

How Can I replace AsyncTask or Retrofit Callbacks with Coroutines ?

And there were lot of articles who wanted me to understand the LiveData and MVVM Architecture.

First, You need to Implement coroutines in your project

I Uploaded this project on GitHub :

//Coroutine Dependency
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.0'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.0'

And for network call I am using Retrofit

//Retrofit Dependency
implementation 'com.squareup.retrofit2:retrofit:2.3.0'
implementation 'com.squareup.retrofit2:converter-gson:2.3.0'
implementation 'com.jakewharton.picasso:picasso2-okhttp3-downloader:1.1.0'

I am going to call 2 API to get 2 different string and merge it to show on UI.

https://cchat.in/SampleAPI/getMessage1.php // response: {"message":"This is first api call"}https://cchat.in/SampleAPI/getMessage2.php// response: {"message":"This is Second api call"}

On Click of “Click Me” button 2 API Calls will happen and merged it to one single text.

Here, we started coroutineScope:

coroutineScope.async(Dispatchers.Default) {
getText(visibleTextView)
}
// This will tell compiler to run the task in Different Thread.

In getText function we are doing 2 network call and we declared the function as suspended:

suspend fun getText(textView1: TextView) {
var text1 = fromUrl1()
var text2 = fromUrl2()
coroutineScope.async (Dispatchers.Main)
{
textView1.setText(text1+text2)
}

}
suspend fun fromUrl1(): String {
val service = RetrofitClientInstance.retrofitInstance?.create(GetDataService::class.java)
service?.apply {
val
call: Call<KotlinDataClass?>? = this.getUrl1
call?.execute()?.body()?.message?.let {
return it
}
}


return "call failed"
}

suspend fun fromUrl2(): String {
val service = RetrofitClientInstance.retrofitInstance?.create(GetDataService::class.java)
val call: Call<KotlinDataClass?>? = service!!.getUrl2
call?.execute()?.body()?.message?.let {
return it
}

return "call failed"
}

Note: other 2 function i.e fromUrl1 and fromUrl2 is also suspended.

As you can see we just called :

call?.execute()?.body()?.message?.let {
return it
}
// Calling execute() function directly as we are not on main thread. If the result is positive the "return it" will be called else "call failed"

Now, In get text function we are calling 2 network request and both will execute sequentially. There is no need to implement callbacks. Coroutines will do that for you.

var text1 = fromUrl1() // will execute sequentially
var text2 = fromUrl2()
coroutineScope.async (Dispatchers.Main)
{
textView1.setText(text1+text2) // Calling on Main Thread
}

Volla! Now you can call multiple network request and that can be dependent on each other and they can execute sequentially:

For Example, What if you want to call an API to get music Raw data and then send it to another API to process it. In this particular scenario you need to wait for First to complete Second. This can be done via coroutines in a very efficient way.

And if you look at the code above. It looks cleaner and more readable then “Asynctask or Retrofit Enqueue Methods Callback”

--

--

Lokesh Deshmukh
Prog-Ramming Solutions

Android developer | Photographer | Web Deveoper| Freelancer