anko bg 的替代品是什么?
What is the replacement of anko bg?
我正在使用 anko bg 功能来管理后台任务。
代码如下
import com.github.kittinunf.fuel.core.FuelError
import com.github.kittinunf.fuel.core.Request
import com.github.kittinunf.fuel.core.Response
import com.github.kittinunf.result.Result
import org.jetbrains.anko.coroutines.experimental.bg
object AuthenticationService {
suspend fun login(login: Login): Triple<Request, Response, Result<GenResponse, FuelError>> {
return bg {
HttpService.post<GenResponse>("/auth/login",login.toJsonString())
}.await()
}
}
它显示 bg 已被弃用并要求我使用 async(block)
如何用异步替换这个后台任务?
考虑为不同类型的后台任务创建您自己的 ThreadPoolExecutor
s 并将它们用作协程调度程序。
而且你随时可以看看Anko bg
source code作为参考。
您不应使用任何替代 bg
,因为 Fuel 支持异步 HTTP。您不需要任何后台线程来执行请求。此外,Fuel 有 first-class support for coroutines.
根据信息,您应该将其替换为协同程序的异步。
可能是以下几行:
return async(Dispatchers.IO) {
HttpService.post<GenResponse>("/auth/login",login.toJsonString())
}.await()
如果有帮助请告诉我:)!
另一种方法
suspend fun login(login: Login): Triple<Request, Response, Result<GenResponse, FuelError>> {
return withContext(Dispatchers.Default) {
HttpService.postNoAuth<GenResponse>("/auth/login", login.toJsonString())
}
}
我正在使用 anko bg 功能来管理后台任务。
代码如下
import com.github.kittinunf.fuel.core.FuelError
import com.github.kittinunf.fuel.core.Request
import com.github.kittinunf.fuel.core.Response
import com.github.kittinunf.result.Result
import org.jetbrains.anko.coroutines.experimental.bg
object AuthenticationService {
suspend fun login(login: Login): Triple<Request, Response, Result<GenResponse, FuelError>> {
return bg {
HttpService.post<GenResponse>("/auth/login",login.toJsonString())
}.await()
}
}
它显示 bg 已被弃用并要求我使用 async(block)
如何用异步替换这个后台任务?
考虑为不同类型的后台任务创建您自己的 ThreadPoolExecutor
s 并将它们用作协程调度程序。
而且你随时可以看看Anko bg
source code作为参考。
您不应使用任何替代 bg
,因为 Fuel 支持异步 HTTP。您不需要任何后台线程来执行请求。此外,Fuel 有 first-class support for coroutines.
根据信息,您应该将其替换为协同程序的异步。 可能是以下几行:
return async(Dispatchers.IO) {
HttpService.post<GenResponse>("/auth/login",login.toJsonString())
}.await()
如果有帮助请告诉我:)!
另一种方法
suspend fun login(login: Login): Triple<Request, Response, Result<GenResponse, FuelError>> {
return withContext(Dispatchers.Default) {
HttpService.postNoAuth<GenResponse>("/auth/login", login.toJsonString())
}
}