I have the following code in my repository:
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)
}
})
}
It works so far, but I'd like to know if using AsyncTask
like this can lead to any problems. I heard about AsyncTask
being a bad solution that is soon to be deprecated by Google so I'm in doubt. If theres any other alternative to execute my request on a background thread I would appreciate if you could indicate it.
I dont want to use Retrofit or any similar alternatives.