RxJava:如何拆分单

时间:2018-07-05 20:54:58

标签: kotlin rx-java

如何将单个流拆分为单独的单个流,这样就可以执行以下操作而无需两次计算getUserId()

// getUserId() returns Single<String>

getUserId().flatMap { getSomething(it) }  // Return Single<Something>
getUserId().flatMap { getSomethingElse(it) } // Return Single<SomethingElse>

1 个答案:

答案 0 :(得分:2)

使用getUserId缓存cache的结果

val userIdCached = getUserId().cache()
userIdCached
    .flatMap { getSomething(it) }
    .subscribe(...)
userIdCached
    .flatMap { getSomethingElse(it) }
    .subscribe(...)