Retrofit 的 `enqueue()` 没有及时为 return 语句重新分配对象的值?
Retrofit's `enqueue()` doesn't re-assign value of object in time for the return statement?
我正在调用 API 并将响应主体分配给 Retrofit 的 enqueue()
内的一个对象,问题是入队完成得太快,无法分配之前的值函数体的return语句被调用。
以前,我之前使用过 MutableLiveData
并且它处理了这一点,因为它总是观察数据并且当它更改时它会毫无问题地分配它但现在我不想使用任何 MutableLiveData 或Observables 因为我试图在任何 UI 实际绘制在屏幕上之前准备数据。
fun getResponse(
weatherLocationCoordinates: WeatherLocation
): RequestResponse {
weatherApiService.getCurrentWeather(
weatherLocationCoordinates.latitude,
weatherLocationCoordinates.longitude
).enqueue(object : Callback<WeatherResponse> {
override fun onResponse(
call: Call<WeatherResponse>,
response: Response<WeatherResponse>
) {
if (response.isSuccessful) {
// This where I do the assigning
requestResponse = RequestResponse(response.body(), true)
}
}
override fun onFailure(call: Call<WeatherResponse>, t: Throwable) {
requestResponse = RequestResponse(null, false)
}
})
// When this is called, enqueue is still not finished
// therefore I get the wrong value, I get the previously set initialization value of the obj.
return requestResponse
}
我应该使用回调还是其他方式?我不确定如何实现回调。
跟进这里的评论是一种回调方法:
假设我们将方法签名更改为:
fun getResponse(
weatherLocationCoordinates: WeatherLocation,
onSuccess: (WeatherResponse) -> Unit = {},
onError: (Throwable) -> Unit = {}
) {
weatherApiService.getCurrentWeather(
weatherLocationCoordinates.latitude,
weatherLocationCoordinates.longitude
).enqueue(object : Callback<WeatherResponse> {
override fun onResponse(
call: Call<WeatherResponse>,
response: Response<WeatherResponse>
) {
if (response.isSuccessful) {
onSuccess(response.body())
} else {
onError(CustomHttpExceptionWithErrorDescription(response))
}
}
override fun onFailure(call: Call<WeatherResponse>, t: Throwable) {
onError(t)
}
})
}
CustomHttpExceptionWithErrorDescription
必须是您编写的代码,可以简单地解析从服务器获得的错误。任何非 2XX 状态码
此方法接受 2 个额外参数 - 一个在成功时调用,另一个在出错时调用。这个想法是这样称呼它:
getResponse(
weatherLocationCoordinates,
onSuccess = {
// do something with response
},
onError = {
// do something with the error
}
)
因为它们有默认参数,所以您实际上不需要指定这两个回调。就是您感兴趣的那个。示例:
// react only to successes
getResponse(
weatherLocationCoordinates,
onSuccess = {
// do something with response
}
)
// react only to errors
getResponse(weatherLocationCoordinates) {
// do something with the error
}
// just call the network calls and don't care about success or error
getResponse(weatherLocationCoordinates)
我正在调用 API 并将响应主体分配给 Retrofit 的 enqueue()
内的一个对象,问题是入队完成得太快,无法分配之前的值函数体的return语句被调用。
以前,我之前使用过 MutableLiveData
并且它处理了这一点,因为它总是观察数据并且当它更改时它会毫无问题地分配它但现在我不想使用任何 MutableLiveData 或Observables 因为我试图在任何 UI 实际绘制在屏幕上之前准备数据。
fun getResponse(
weatherLocationCoordinates: WeatherLocation
): RequestResponse {
weatherApiService.getCurrentWeather(
weatherLocationCoordinates.latitude,
weatherLocationCoordinates.longitude
).enqueue(object : Callback<WeatherResponse> {
override fun onResponse(
call: Call<WeatherResponse>,
response: Response<WeatherResponse>
) {
if (response.isSuccessful) {
// This where I do the assigning
requestResponse = RequestResponse(response.body(), true)
}
}
override fun onFailure(call: Call<WeatherResponse>, t: Throwable) {
requestResponse = RequestResponse(null, false)
}
})
// When this is called, enqueue is still not finished
// therefore I get the wrong value, I get the previously set initialization value of the obj.
return requestResponse
}
我应该使用回调还是其他方式?我不确定如何实现回调。
跟进这里的评论是一种回调方法:
假设我们将方法签名更改为:
fun getResponse(
weatherLocationCoordinates: WeatherLocation,
onSuccess: (WeatherResponse) -> Unit = {},
onError: (Throwable) -> Unit = {}
) {
weatherApiService.getCurrentWeather(
weatherLocationCoordinates.latitude,
weatherLocationCoordinates.longitude
).enqueue(object : Callback<WeatherResponse> {
override fun onResponse(
call: Call<WeatherResponse>,
response: Response<WeatherResponse>
) {
if (response.isSuccessful) {
onSuccess(response.body())
} else {
onError(CustomHttpExceptionWithErrorDescription(response))
}
}
override fun onFailure(call: Call<WeatherResponse>, t: Throwable) {
onError(t)
}
})
}
CustomHttpExceptionWithErrorDescription
必须是您编写的代码,可以简单地解析从服务器获得的错误。任何非 2XX 状态码
此方法接受 2 个额外参数 - 一个在成功时调用,另一个在出错时调用。这个想法是这样称呼它:
getResponse(
weatherLocationCoordinates,
onSuccess = {
// do something with response
},
onError = {
// do something with the error
}
)
因为它们有默认参数,所以您实际上不需要指定这两个回调。就是您感兴趣的那个。示例:
// react only to successes
getResponse(
weatherLocationCoordinates,
onSuccess = {
// do something with response
}
)
// react only to errors
getResponse(weatherLocationCoordinates) {
// do something with the error
}
// just call the network calls and don't care about success or error
getResponse(weatherLocationCoordinates)