使用其他可观察的数据

问题描述 投票:0回答:1

我无法进入ReactiveX的心态,或者我正在使用的代码库编写得很差。

假设我有一些Observable A(),并且我需要另一个Observable B()的数据,以便对通过A的数据进行验证,我该如何在RxJava中完成此操作(宁愿使用RxKotlin实现)。请注意,A和B都返回对象列表的Single。

fun B(): Single<List<Bar>> {
  ...
}

fun A() : Single<List<Foo>> {
  Single.just(readRecords()).map { record ->
    // val bar = B.getAll()??? This seems like an anti-pattern and I'm not sure if it would necessarily be right to .subscribe()???

    if (bar.contains(record)) {
      // ... some validation
    }
  }
}
rx-java rx-kotlin
1个回答
0
投票

因此您可以使用.map {}运算符:

fun A() : Single<List<Foo>> {
    return B.getAll().map { allFromB ->  
        val record = readRecords()
        if (allFromB.contains(record)) {
           // ... some validation
        }
        ...
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.