给定一个返回模型的API(由Retrofit实现)。我使用扩展函数将旧的Call包装成Deferred
:
fun <T> Call<T>.toDeferred(): Deferred<T> {
val deferred = CompletableDeferred<T>()
// cancel request as well
deferred.invokeOnCompletion {
if (deferred.isCancelled) {
cancel()
}
}
enqueue(object : Callback<T> {
override fun onFailure(call: Call<T>?, t: Throwable) {
deferred.completeExceptionally(t)
}
override fun onResponse(call: Call<T>?, response: Response<T>) {
if (response.isSuccessful) {
deferred.complete(response.body()!!)
} else {
deferred.completeExceptionally(HttpException(response))
}
}
})
return deferred
}
现在我可以得到这样的模型:
data class Dummy(val name: String, val age: Int)
fun getDummy(): Deferred<Dummy> = api.getDummy().toDeferred()
但是如何修改Deferred
中的对象并返回Deferred
:
fun getDummyAge(): Deferred<Int> {
// return getDummy().age
}
我是协同程序的新手,所以可能这不是这里的事情。假设我是一个RxJava粉丝我会实现这样的情况:
fun getDummy(): Single<Dummy> = api.getDummy().toSingle()
fun getDummyAge(): Single<Int> = getDummy().map { it.age }
那么我应该尝试从Deferred
函数返回getDummyAge
吗?或者可能最好尽可能地声明一个suspended fun
并在我所有api的方法上调用deferred.await()
?
如果您遵循异步编程风格,即编写返回Deferred<T>
的函数,那么您可以像这样定义async函数getDummyAge
:
fun getDummyAge(): Deferred<Int> = async { getDummy().await().age }
但是,这种编程风格一般不建议在Kotlin中使用。惯用的Kotlin方法是使用以下签名定义暂停扩展函数Call<T>.await()
:
suspend fun <T> Call<T>.await(): T = ... // more on it later
并使用它来编写暂停函数getDummy
,它直接返回Dummy
类型的结果,而不将其包装为延迟:
suspend fun getDummy(): Dummy = api.getDummy().await()
在这种情况下,您可以轻松编写暂停函数getDummyAge
:
suspend fun getDummyAge(): Int = getDummy().age
对于Retrofit调用,您可以像这样实现await
扩展:
suspend fun <T> Call<T>.await(): T = suspendCancellableCoroutine { cont ->
cont.invokeOnCompletion { cancel() }
enqueue(object : Callback<T> {
override fun onFailure(call: Call<T>?, t: Throwable) {
cont.resumeWithException(t)
}
override fun onResponse(call: Call<T>?, response: Response<T>) {
if (response.isSuccessful) {
cont.resume(response.body()!!)
} else {
cont.resumeWithException(HttpException(response))
}
}
})
}
如果你想更多地了解异步和暂停功能之间的风格差异,那么我建议你观看2017年KotlinConf的Introduction to Coroutines。如果你喜欢简短的阅读,那么this section from the design document也会提供一些见解。