[英]retrofit2 with coroutine or not, what's the difference?
when I use retrofit2 with no coroutine, the result is null.当我使用没有协程的改造2时,结果为空。 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.
但是我使用 mutablelivedata 发现了一些奇怪的东西,结果是正确的。
retrofit2 with coroutine使用协程改造2
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没有协程的改造2
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) mutablelivedata 的奇怪结果(另一个项目代码)
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.
将此代码与没有协程代码的retrofit2进行比较,区别仅在于是否可变livedata。 do I have to use asyncronouse library or livedata?
我必须使用 asyncronouse 库还是 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.但是,写入日志的代码的
respnose
是完全不同的对象。
with coroutine, response
is DetailEntity
使用协程,
response
为DetailEntity
with no coroutine, response
is Response<DetailEntity>
没有协程,
response
是Response<DetailEntity>
override fun onResponse(call: Call<DetailEntity>, response: Response<DetailEntity>){
if(response.body()!=null) {
Log.d("TAG",response.body()!!.toString())
data = response.body()!!
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.