rx-java - 如何使用 RxJava2 创建谓词
问题描述
我正在尝试学习 Rxjava,但仍然处于非常基础的水平。我正在寻找有关以下问题的一些指导。
我有这两个 Observables,
fun getPlayheadPositionInMilliseconds(): Observable<Long> {
return ConnectableObservable.interval(1000, TimeUnit.MILLISECONDS)
.map { exoPlayer.currentPosition }
.publish()
.autoConnect()
}
fun getContentDurationInMilliseconds(): Observable<Long> {
return ConnectableObservable.just(exoPlayer.duration)
.publish()
.autoConnect()
}
现在我正在尝试从这些中创建一个谓词。我的要求是当getPlayheadPositionInMilliseconds
达到 70% 时 getContentDurationInMilliseconds
我想发出一个布尔信号。
如您所见,getPlayheadPositionInMilliseconds
每 1000 毫秒后,订阅者将获得一个新值,现在我想将此值与我从中获得的总持续时间进行比较getContentDurationInMilliseconds
。当getPlayheadPositionInMilliseconds
值达到 70% 时getContentDurationInMilliseconds
,将产生一个布尔信号。
我知道如何在不使用 RxJava 的情况下做到这一点,而是在寻找一种在 RxJava 中做到这一点的方法。如果需要更多信息,请告诉我。
解决方案
如果我理解正确,您希望Observable<Boolean>
每次发出一个子可观察对象时都会发出一个,并且根据您的谓词使布尔值为真。这可以通过以下方式实现:
// Emits items when either child observable emits
fun isPlayheadPosition70PercentOfContentDuration(): Observable<Boolean> =
Observables // Helper class from RxKotlin package
.combineLatest( // Emit a Pair<Long, Long> of values from the
getPlayheadPositionInMilliseconds(), // latest of the child observables
getContentDurationInMilliseconds()
)
.map { (playheadPosition, contentDuration) -> // Transform the item based on this function
playheadPosition >= 0.7 * contentDuration // (in this case, into a Boolean based on a predicate)
}
哪里有一个带有来自RxKotlinObservables
的有用方法的帮助类,在 Kotlin 中编写 Rx 时,这是一个不错的选择。
我想也许你也可能只想在你的谓词得到满足后立即触发一个事件。在这种情况下,您可以将上述内容转换为Completable
当您的谓词首次变为真时触发的 a:
// If you subscribe to this, the onComplete will signal once the predicate is satisfied
fun playheadPositionHasReached70PercentContentDuration(): Completable =
Observables
.combineLatest(getPlayheadPositionInMilliseconds(), getContentDurationInMilliseconds())
// Filter: Only allow elements through the stream if they satisfy this predicate
.filter { (playheadPosition, contentDuration) -> playheadPosition >= 0.7 * contentDuration }
// Take only the first element (i.e. turn this into a Single)
.firstOrError()
// Actually, I don't care about the element, I just want to know when it happens
// (i.e. turn this into a Completable)
.ignoreElement()
推荐阅读
- javascript - ChannelSearch.jsx 中的意外令牌,预期为“...”
- r - 试图在 R 中转换为日期,但它只留下 NA 值?我该怎么办?
- r - 闪亮的条件面板无法在多个条件下使用 uiOutput
- angular - @firebase/firestore:Firestore (9.0.0):无法访问 Cloud Firestore 后端(权限被拒绝)
- javascript - 如何添加加载更多按钮以使用 jquery 或 ajax 在我的 django 项目模板中显示更多评论?
- install4j - 在 mac 和 linux 上 Install4j 后台更新程序失败
- c - 尝试使用 malloc 做 3 维矩阵
- mysql - 当我试图在 xampp 中启动 MySQL 时。我收到此错误
- c# - 允许 EFCore [必需]-Attribute 可以为空
- python - Robotframework:找不到自定义库方法关键字