简体   繁体   中英

retrofit2 with coroutine or not, what's the difference?

when I use retrofit2 with no coroutine, the result is null. but when using that with coroutine, the result is right. I think it's the problem of syncronization. but I found something strange using mutablelivedata, the result is right.

retrofit2 with coroutine

    override suspend fun getRetrofit(id : Int): DetailEntity {

        withContext(ioDispatcher){
            val request = taskNetworkSource.searchItem(id)
            val response = request.await()

            if(response.body !=null){
                Log.d("TAG",""+response.toString())
                data = response
            }


        }
        return data
    }

good result

D/TAG: DetailEntity(body=DetatilItem(oily_score=6, full_size_image=url, price=54840, sensitive_score=76, description=description, id=5, dry_score=79, title=title), statusCode=200)

retrofit2 with no coroutine

       override suspend fun getRetrofit(id : Int): DetailEntity {

        taskNetworkSource.searchItem(id).enqueue(object: Callback<DetailEntity> {
            override fun onFailure(call: Call<DetailEntity>, t: Throwable) {

            }
            override fun onResponse(call: Call<DetailEntity>, response: Response<DetailEntity>){
                if(response.body()!=null) {
                    Log.d("TAG",response.toString())
                    data = response.body()!!

                }

            }
        })

        return data
    }

bad result

D/TAG: Response{protocol=h2, code=200, message=, url=https://6uqljnm1pb.execute-api.ap-northeast-2.amazonaws.com/prod/products/5}

strange result with mutablelivedata(another project code)

    lateinit var dataSet : DetailModel             
    var data = MutableLiveData<DetailModel>()       

    fun getDetailRetrofit(id:Int) : MutableLiveData<DetailModel>{          
        Retrofit2Service.getService().requestIndexItem(id).enqueue(object:
            Callback<DetailResponse> {
            override fun onFailure(call: Call<DetailResponse>, t: Throwable) {    

            }
            override fun onResponse(call: Call<DetailResponse>, response: Response<DetailResponse>) {   
                if(response.body()!=null) {
                    var res = response.body()!!.body
                    dataSet = DetailModel( res.get(0).discount_cost,
                        res.get(0).cost,
                        res.get(0).seller,
                        res.get(0).description+"\n\n\n",
                        res.get(0).discount_rate,
                        res.get(0).id,
                        res.get(0).thumbnail_720,
                        res.get(0).thumbnail_list_320,
                        res.get(0).title
                    )
                    data.value = dataSet
                }
            }
        })
        return data
    }

and this another project code result is right. comparing this code to retrofit2 with no coroutine code, the difference is only mutablelivedata or not. do I have to use asyncronouse library or livedata?

added

data class DetailEntity(val body: DetatilItem,
                        val statusCode: Int = 0)

data class DetatilItem(val oily_score: Int = 0,
                       val full_size_image: String = "",
                       val price: String = "",
                       val sensitive_score: Int = 0,
                       val description: String = "",
                       val id: Int = 0,
                       val dry_score: Int = 0,
                       val title: String = "")

retrofit with no coroutine it seem to be no problem.

But, respnose at your code written to log are the completely different object.

with coroutine, response is DetailEntity

with no coroutine, response is Response<DetailEntity>

if you want same log print, try as below

override fun onResponse(call: Call<DetailEntity>, response: Response<DetailEntity>){
    if(response.body()!=null) {
        Log.d("TAG",response.body()!!.toString())
        data = response.body()!!
    }
}

Reference

Retrofit - Response<T>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM