tom*_*mmy 5 database android coroutine kotlin kotlin-coroutines
我正在使用 Kotlin 协程从服务器获取数据,并将延迟传递给其他函数。如果服务器在 2000 毫秒内没有给出答案,我想从本地 Room DB 检索对象(如果它存在于本地数据库中),但如果我最终从服务器收到数据,我想保存在在本地数据库中以供将来调用。我怎样才能做到这一点?我想过使用withTimeout,但是在这种情况下,超时后没有等待服务器的响应。
override fun getDocument(): Deferred<Document> {
return GlobalScope.async {
withTimeoutOrNull(timeOut) {
serverC.getDocument().await()
} ?: dbC.getDocument().await()
}
}
Run Code Online (Sandbox Code Playgroud)
我想到的一个想法:
fun getDocuments(): Deferred<Array<Document>> {
return GlobalScope.async {
val s = serverC.getDocuments()
delay(2000)
if (!s.isCompleted) {
GlobalScope.launch {
dbC.addDocuments(s.await())
}
val fromDb = dbC.getDocuments().await()
if (fromDb != null) {
fromDb
} else {
s.await()
}
} else {
s.await()
}
}
}
Run Code Online (Sandbox Code Playgroud)
我建议使用库select中的表达式kotlinx.coroutines。
https://kotlinlang.org/docs/reference/coroutines/select-expression.html
fun CoroutineScope.getDocumentsRemote(): Deferred<List<Document>>
fun CoroutineScope.getDocumentsLocal(): Deferred<List<Document>>
@UseExperimental(ExperimentalCoroutinesApi::class)
fun CoroutineScope.getDocuments(): Deferred<List<Document>> = async {
supervisorScope {
val documents = getDocumentsRemote()
select<List<Document>> {
onTimeout(100) {
documents.cancel()
getDocumentsLocal().await()
}
documents.onAwait {
it
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
选择表达式可以通过onAwait来自网络的信号或超时来恢复。在这种情况下我们返回本地数据。
您可能还想以块的形式加载文档,因为这Channel也可能有帮助
https://kotlinlang.org/docs/reference/coroutines/channels.html
最后,我们在示例中使用了 kotlinx.coroutines 的实验性 API,该功能onTimeout可能会在库的未来版本中发生变化
| 归档时间: |
|
| 查看次数: |
1455 次 |
| 最近记录: |