【发布时间】:2019-09-11 04:21:38
【问题描述】:
我有一个关于何时使用暂停和何时不使用的问题。我的 APIRepo 类有这样的功能 -
override suspend fun retrieveDataFromRemote(): MyResult {
if (utility.checkDeviceInternetConnection()) {
try {
val result = remoteInterface.getData().await()
return ...
} catch (t: Throwable) {
return ...
}
} else {
return ...
}
}
我的远程接口代码如下所示 -
@GET("/data/mydata")
fun getData(): Deferred<Response<MyModel>>
如您所见,我的RemoteInterface 没有suspend 关键字并返回Deferred。这工作得很好。但是当我将suspend 关键字添加到getData() 时,我没有得到API 响应。为什么会这样?和Deferred 有关系吗?
【问题讨论】:
标签: android kotlin kotlin-coroutines