如何将单个流拆分为单独的单个流,这样就可以执行以下操作而无需两次计算getUserId()
?
// getUserId() returns Single<String>
getUserId().flatMap { getSomething(it) } // Return Single<Something>
getUserId().flatMap { getSomethingElse(it) } // Return Single<SomethingElse>
答案 0 :(得分:2)
使用getUserId
缓存cache
的结果
val userIdCached = getUserId().cache()
userIdCached
.flatMap { getSomething(it) }
.subscribe(...)
userIdCached
.flatMap { getSomethingElse(it) }
.subscribe(...)