0

我有多个Observable<Boolean>来自“警报传感器”的传递数据。他们只提供价值变化。如何等到所有人都切换到false表明不再有警报的情况?

4

2 回答 2

2

Observable.combineLatest()与该场景结合使用Observable.filter()

Observale<Boolean> source1 = TODO();
Observale<Boolean> source2 = TODO();
Observale<Boolean> source3 = TODO();
Observable
    .combineLatest(source1, source2, source3, (value1. value2, value3) -> {
        return value1 || value2 || value3;
    })
    .filter(combinedValue -> combinedValue == false)
    .subscribe(TODO())
于 2020-05-21T19:01:38.263 回答
0

@ConstOrVar 的回答对我帮助很大,但实际上我必须向其中添加一个元素。因为我Observables只在实际值更改时提供新状态,所以我必须确保在combineLatest() 中有一些参考初始状态可以操作:

    val oneTruth = Observable.just(true)

    Observables
        .combineLatest(
            oneTruth.concatWith(events.cliffLeft),
            oneTruth.concatWith(events.cliffFrontLeft),
            oneTruth.concatWith(events.cliffFrontRight),
            oneTruth.concatWith(events.cliffRight),
            oneTruth.concatWith(events.wheelDropLeft),
            oneTruth.concatWith(events.wheelDropRight)
        ) { v0, v1, v2, v3, v4, v5 ->
          (v0 || v1 || v2 || v3 || v4 || v5)
        }
        .filter { danger -> !danger }
        .filter { state.oiMode == OiMode.PASSIVE }
        .subscribe {
          logger.debug { "Bringing Roomba back to safe mode" }
          roomba.safeMode()
        }

rxkotlin注意:这是使用语法糖的 Kotlin 代码combineLatest

于 2020-05-22T10:01:17.487 回答