rxjava合并不同类型的可观察对象 [英] rxjava merge observables of different type

查看:300
本文介绍了rxjava合并不同类型的可观察对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是rxjava的新手.我需要结合两个发出不同类型对象的可观察对象.类似于Observable<Milk>Observable<Cereals>并得到Observable<CerealsWithMilk>.我找不到类似这样的运算符.做这样的事情的RX方式是什么?请注意,MilkCereals是异步的.

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.

combineLatestzip相似,不同之处在于,即使只是一个新的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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆