java - 如何调用返回 Observable 的多个 API并将他们的响应汇总到单个对象
问题描述
我必须创建一个名为 say Product.java的聚合 POJO ,通过调用多个源/Rest API(#sources 可以是任何数字)并解析它们的各个响应(Observable<Response>)以填充 Product.java 中的相关字段
Observable.zip() 有点适合我的要求,但它需要一个固定的编号。args,而在我的情况下,#sources (args) 可以是任何可配置的数字。
有没有通用/更清洁的方法来做到这一点?
List<Source> sources = //Some configuration, where sources.size()
// Call all source REST APIs in parallel where each returns an Observable<Response>
// Parse each Observable<Response> & populate the relevant fields in a shared Product.java Object. (No two responses will try to populate the same field in Product)
解决方案
如果顺序不重要,只需 .merge() 和 .scan(); 如果它很重要,请使用 .concat() 而不是 .merge()
final Observable<MyContainer> myContainer = Observable
.merge(sources)
.scan(new MyContainer(), (myContainer, newProduct) -> {
// add newProduct to myContainer
return myContainer;
})
推荐阅读
- android - 面对 Flutter 中的按钮 onclick 问题
- google-cloud-platform - Google Cloud:我的用户看不到新的共享 BigQuery 数据集和 GCP 项目
- javascript - 如何使用 gulp 正确包含作为 NPM 包安装的 CSS&JS 文件?
- java - 错误:java.lang.NullPointerException:尝试在空对象引用上调用接口方法“int java.util.List.size()”
- java - 错误共享并将元素添加到队列
- python - 提高对和的速度 - Codewars
- android - 在 Android 中为每个用户创建房间数据库
- ios - 元数据被拒绝 附表 2,第 3.8(b) 节
- oracle - 无法连接到在 Docker 中的 Oracle 数据库 18 XE 上创建的新用户
- mysql - 错误 1366 (HY000):不正确的整数值:第 1 行的列“Industry_Code”的“6029”不知道这是为什么?