将AsyncTask与可运行的[重复项]一起使用

问题描述 投票:0回答:2
我的存储库中有以下代码:

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弃用,因此我对此表示怀疑。如果还有其他选择可以在后台线程上执行我的请求,请指出。

我不想使用翻新或任何类似的替代方法。

android kotlin android-asynctask
2个回答
1
投票

0
投票
© www.soinside.com 2019 - 2024. All rights reserved.