class BusRepositoryImpl(
private val busService: BusService
) : BusRepository
override fun get(id: String, callback: Callback<BusItem>){
AsyncTask.execute {
try {
val apiResponse = busService.get(id)
callback.onResponse(apiResponse.bus)
}
catch (e: Exception){
callback.onFailure(e)
}
}
}
}
busService:
class BusServiceImpl(private val baseUrl: String, private val httpClient: HttpClient) override fun get(id: String): ApiResponse { return httpClient.get("${baseUrl}/bus/$id") }
ViewModel:
fun get(id: String) { busRepository.get(id, object: Callback<BusItem>{ override fun onResponse(data: BusItem) { busLiveData.postValue(data) } override fun onFailure(t: Throwable) { errors.postValue(t) } }) }
到目前为止,它仍然有效,但是我想知道是否像这样使用AsyncTask
会导致任何问题。我听说AsyncTask
是一个不好的解决方案,很快就会被Google弃用,因此我对此表示怀疑。如果还有其他选择可以在后台线程上执行我的请求,请指出。我不想使用翻新或任何类似的替代方法。