android - How to create a flow which detects consecutive value increments of another flow?
问题描述
I have a hot flow fooFlow
that emits integer values. How can I construct another hot flow barFlow
that only emits values when fooFlow
emits a larger value than the most recent value emitted by fooFlow
? In a sense, barFlow
detects consecutive value increases of fooFlow
.
Example: If fooFlow
emits (4, 2, 7, 3, 3, 1, 2, 4, ...), then barFlow
emits (7, 2, 4, ...).
解决方案
There might be a more natural or cleaner looking way to do this, but this is my first instinct:
val barFlow: SharedFlow<Int> = MutableSharedFlow<Int>().also { outflow ->
var previousValue = Int.MAX_VALUE
fooFlow.onEach { newValue ->
if (newValue > previousValue) {
outflow.emit(newValue)
}
previousValue = newValue
}.launchIn(viewModelScope)
}
推荐阅读
- python - pandas 按 2 列中的值过滤,返回整行
- python - Python - 在比较中使用时默认获取枚举值
- shell - 将 crontab 标准输出重定向到标准输出而不是默认电子邮件
- angular - 使用 Angular 在内容初始化后执行 js 代码
- c++ - 什么是 Eigen C++ 中 MKL 的函数“zgges”的等价物?
- c# - 每隔 5 分钟在窗体之间自动随机播放一次
- android - 有时 !transformDexArchiveWithExternalLibsDexMergerForDebug
- java - 等待返回 DeferredResult 的方法
- javascript - foreach 中的 return 语句总是返回 undefined
- android - Android 以编程方式添加图像以查看此错误 java.lang.StackOverflowError: stack size 8MB