```suspend fun getTopKeys(con: Context, rootObject...
# coroutines
d
Copy code
suspend fun getTopKeys(con: Context, rootObject: JSONObject): MutableLiveData<AuthResponse> {

    return withContext(<http://Dispatchers.IO|Dispatchers.IO>){
        val gson = Gson()

        val accessToken = MyPrefConstant.getAccessToken(con)

        val retrofit = RetrofitConfig.getRetrofitWithAccessToken(accessToken, con)

        val service = retrofit?.create(ApiInterface::class.java)

        var data = AesKotlin.encrypt(rootObject!!.toString(),AppConfig.ENCRYPTION_KEY)

        Log.e("App1>>>>", data.replace("[\n]", "")!!.toString())

        val call = service?.getTopKeys(data.replace("[\n]", "")!!.toString())

        call?.enqueue(object : Callback<AuthResponse> {
            override fun onResponse(call: Call<AuthResponse>, response: Response<AuthResponse>) {

                if (response.code() == 200 && response.body().status.equals("OK")) {
                    val responseData = response.body()!!
                    searchData.value = gson.fromJson(gson.toJson(responseData), AuthResponse::class.java)

                    val i = Intent("return.apiResponse")
                    i.putExtra("data",  gson.toJson(responseData).toString())
                    i.putExtra("method", "getTopN")
                    //con.sendBroadcast(i)
                    LocalBroadcastManager.getInstance(con).sendBroadcast(i)
                }else{
                    //  Toast.makeText(con, response.body().errorMessage, Toast.LENGTH_LONG).show()
                    val json = "{\"Status\": \"NOT OK\"," +
                            " \"ErrorMessage\": \"Something went wrong\" ," +
                            " \"Data\": \"NULL\", " +
                            " \"Timestamp\": \"NULL\" }"

                    searchData.value = gson.fromJson(json, AuthResponse::class.java)
                }
            }

            override fun onFailure(call: Call<AuthResponse>, t: Throwable) {
                Log.e("API","Error")
                val json = "{\"Status\": \"NOT OK\"," +
                         " \"ErrorMessage\": \"Something went wrong \"," +
                        " \"Data\": \"NULL\", " +
                        " \"Timestamp\": \"NULL\" }"
                searchData.value = gson.fromJson(json, AuthResponse::class.java)

            }
        })
        searchData
    }

}
In above method getTopKeys following methods are showing in mainthread in profiler
getJSON() - Related to gson
setValue - setting value to livedata
fromJSON - Related to gson
j
You're using a callback-based API from retrofit. Thus, no matter what dispatcher you're using to make the call, it doesn't control on what thread the callbacks will be called (that's for retrofit to decide). I'm not an Android nor retrofit expert, but I would assume the callbacks are called on the main thread, and that would explain what you're seeing. You should look into using suspend APIs from retrofit (I think they provide those as well). If you can't, you can also wrap any callback based API into a suspend API by using
suspendCancellableCoroutine
r
also if you want callback api’s to send response in a specific thread. Retrofit’s builder takes
callbackExecutor