RxJava 3 映射器
RxJava 3 Mapper
我的项目用的是RxJava3,写不了请求,折腾了好几个小时
我有 List 并且有一个函数 returns Single> by Stream,我想得到一个 Single
fun getMockTopics(streamId: Long): Single<List<Topic>> {
return Single.just(listOf(Topic(1, "")))
}
typealias SteamTopics = Map<Stream, List<Topic>>
override fun getTopics(streams: List<Stream?>): Single<SteamTopics> {
return Observable.fromCallable { streams.filterNotNull() }.flatMapIterable { it }
.map { stream ->
Pair(stream, getMockTopics(streamId = stream.streamId))
}.flatMap {
TODO("???")
}
.toMap({ it.first }, { it.second })
}
fun getMockTopics(streamId: Long): Observable<List<Topic>> {
return Observable.just(listOf(Topic(1, "")))
}
override fun getTopics(streams: List<Stream?>): Single<SteamTopics> {
return Observable.fromCallable { streams.filterNotNull() }.flatMapIterable { it }
.flatMap({ stream ->
getMockTopics(streamId = stream.streamId)
}, { stream, topics ->
Pair(stream, topics)
}).toMap({ it.first }, { it.second })
}
我的项目用的是RxJava3,写不了请求,折腾了好几个小时
我有 List> by Stream,我想得到一个 Single
fun getMockTopics(streamId: Long): Single<List<Topic>> {
return Single.just(listOf(Topic(1, "")))
}
typealias SteamTopics = Map<Stream, List<Topic>>
override fun getTopics(streams: List<Stream?>): Single<SteamTopics> {
return Observable.fromCallable { streams.filterNotNull() }.flatMapIterable { it }
.map { stream ->
Pair(stream, getMockTopics(streamId = stream.streamId))
}.flatMap {
TODO("???")
}
.toMap({ it.first }, { it.second })
}
fun getMockTopics(streamId: Long): Observable<List<Topic>> {
return Observable.just(listOf(Topic(1, "")))
}
override fun getTopics(streams: List<Stream?>): Single<SteamTopics> {
return Observable.fromCallable { streams.filterNotNull() }.flatMapIterable { it }
.flatMap({ stream ->
getMockTopics(streamId = stream.streamId)
}, { stream, topics ->
Pair(stream, topics)
}).toMap({ it.first }, { it.second })
}