Zun*_*Zun 2 coroutine kotlin kotlin-flow
我有一个下载文件的流程。该流程发出下载进度。我想在手机通知中心显示进度,但每秒只更新一次值,以防止设备滞后。
我的流程:
return callbackFlow {
someJavaCallback { progress ->
trySend(progress)
}
close()
}
Run Code Online (Sandbox Code Playgroud)
我的 CoroutineWorker,显示通知并下载文件:
myFlow.collect { // update notification }
Result.Success()
Run Code Online (Sandbox Code Playgroud)
我的问题是,如何“限制”收集,例如收集 1%,但 1 秒后它收集 5% 并忽略这两点之间的所有值
fun <T> Flow<T>.throttleLatest(delayMillis: Long): Flow<T> = this
.conflate()
.transform {
emit(it)
delay(delayMillis)
}
Run Code Online (Sandbox Code Playgroud)
来源:https ://github.com/Kotlin/kotlinx.coroutines/issues/1446#issuecomment-1198103541
| 归档时间: |
|
| 查看次数: |
2557 次 |
| 最近记录: |