rxjava合并不同类型的可观察对象 [英] rxjava merge observables of different type
问题描述
我是rxjava的新手.我需要结合两个发出不同类型对象的可观察对象.类似于Observable<Milk>
和Observable<Cereals>
并得到Observable<CerealsWithMilk>
.我找不到类似这样的运算符.做这样的事情的RX方式是什么?请注意,Milk
和Cereals
是异步的.
I'm new to rxjava. I need to combine two observables that emit objects of different type. Something like Observable<Milk>
and Observable<Cereals>
and get a Observable<CerealsWithMilk>
. I couldn't find any operator for something like this. What would be the rx way of doing something like this? Note that Milk
and Cereals
are async.
推荐答案
很难确切地说不知道您需要什么,但是可能 combineLatest().
It's hard to say without knowing exactly what you need, but possibly zip() or combineLatest().
zip
将同时使用Observable<Milk>
和Observable<Cereals>
,并允许您通过提供的功能将它们组合为CerealsWithMilk
.每次您得到两者和 以及 和Cereals
时,都会发出一个新的CerealsWithMilk
.
zip
will take both Observable<Milk>
and Observable<Cereals>
and let you combine them into CerealsWithMilk
via a provided function. This emits a new CerealsWithMilk
each time you get get both a Milk
and a Cereals
.
combineLatest
与zip
相似,不同之处在于,即使只是一个新的Milk
或一个新的Cereals
,它也会发出一个新的CerealsWithMilk
.被发射.
combineLatest
is similar to zip
except it will emit a new CerealsWithMilk
even if just a new Milk
or just a new Cereals
is emitted.
这篇关于rxjava合并不同类型的可观察对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!