Use Retrofit asynchronously or use Coroutines for data fetching from server

I am making a server request to get some data (asynchronously) using Retrofit lib. I have two options

  1. To use Retrofit lib asynchronously by overriding onResponse and onFailure methods as shown below.

var call : Call = RetrofitClientNew.apiInterface.getTehsilNamesGET(districtID.toString())

                call.enqueue(object : Callback<Tehsil1> {
                    override fun onResponse(call: Call<Tehsil1>, response: Response<Tehsil1>) {
                        TODO("Not yet implemented")

                    override fun onFailure(call: Call<Tehsil1>, t: Throwable) {
                        TODO("Not yet implemented")
  1. Use Coroutines (for asynchronous programming) and return JsonObject from Retrofit without overriding onResponse and onFailure methods as shown below.

withContext(Dispatchers.IO) {

      val response : Response<JsonObject?>? = RetrofitClientNew.apiInterface.getTehsilNames(districtID.toString())

        if (response!!.isSuccessful) {


Which approach is better ? Thank you

Whatever suits you best :slight_smile: Assuming that in the second example getTehsilNames() is a suspend function, both these code samples should internally work in a similar way. Sequential code (suspend function) is usually considered to be much easier to handle than callbacks-based code, but on the other hand it requires you to learn and understand coroutines. If this is the only place where you would use coroutines then maybe it is better to go with callbacks, but if you use coroutines already, I suggest going suspend.

1 Like

Thank you for your kind suggestion.

I agree with broot, the best solution is the one that suits you better. I have some expierence with Coroutines because Iā€™m using Firestore Database, and all documentation associated uses Coroutines to deal with Firestore. And in my opinion, the only drawback I had was to understand how to debug a Coroutine, but after understanding Coroutines way of work, it became pretty obvious how to debug it.