java - 链两个答案单
问题描述
我需要链接两个 RX Single 响应 - 改造以获取 ArrayList 与返回 List 的两个响应
我试图用 , 处理这两个答案Map
,Flatmap
但我没有达到我的预期
final ArrayList <List<Consent>> listAllConsents = new ArrayList<>();
Single<List<Consent>> responseDspConsent = subscriptionCenterRemoteDataSource.getConsents(Globals.getAuthorizationTokenUser());
Single<List<Consent>> responseDspConsentByApp = subscriptionCenterRemoteDataSource.getConsentsByApp(Globals.getAuthorizationTokenUser());
responseDspConsentByApp.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread());
responseDspConsent.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.flatMap(consentData -> {
List<Consent> consentList = consentData;
listAllConsents.add(consentList);
return responseDspConsentByApp.map(consentDataByApp -> {
List<Consent> consentListByApp = consentDataByApp;
listAllConsents.add(consentListByApp);
return listAllConsents;
});
})
.subscribe(consentData -> {
Log.v("Entramoss", "Valor: " + listAllConsents.get(0).get(0).getTitle());
paintAllConsents(listAllConsents);
});
我需要在 arrayList 中包含两个响应的所有对象,以便稍后绘制它们。
解决方案
你有两种方法可以做到这一点。
1.您可以使用 Observable.concat(Obs 1, Obs 2)。concat 运算符连接可观察对象并返回一个可观察对象,该可观察对象首先从第一个可观察对象发出项目,然后是第二个可观察对象。来源:http ://reactivex.io/documentation/operators/concat.html
Single<List<Consent>> responseDspConsent = subscriptionCenterRemoteDataSource
.getConsents(Globals.getAuthorizationTokenUser())
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread());
Single<List<Consent>> responseDspConsentByApp = subscriptionCenterRemoteDataSource
.getConsentsByApp(Globals.getAuthorizationTokenUser())
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread());
Observable.concat(responseDspConsent.toObservable(),responseDspConsentByApp.toObservable())
.toList()
.doOnSuccess((list) -> {
paintAllConsents(list);
})
.subscribe();
2.您可以使用 .concatWith 运算符,它与 concat 运算符执行相同的操作,但现在它将一个可观察对象连接到另一个对象,而无需创建新的可观察对象。
Single<List<Consent>> responseDspConsent = subscriptionCenterRemoteDataSource
.getConsents(Globals.getAuthorizationTokenUser())
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread());
Single<List<Consent>> responseDspConsentByApp = subscriptionCenterRemoteDataSource
.getConsentsByApp(Globals.getAuthorizationTokenUser())
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread());
responseDspConsent.concatWith(responseDspConsentByApp)
.toList()
.doOnSuccess((list) -> {
paintAllConsents(list);
})
.subscribe();
推荐阅读
- android - 如何强制媒体流调用流?
- azure-logic-apps - How to read azure service queue message in Logic apps?
- javascript - 如何在 jquery / ajax 调用中正确传递值
- javascript - 未捕获的 SodiumException:标量和点必须是 SODIUM_CRYPTO_SCALARMULT_SCALARBYTES
- intellij-idea - Intellij 无法识别我的步骤定义
- python - ImportError: The _imagingft C module is not installed, Pillow Python3.9 on Raspberry Pi
- python - Python 在 Pycharm 中进行用户数据解析
- node.js - 运行 npm install 时 node-sass 出错
- windows - PyCharm 重新定义了“全部折叠”命令折叠的内容
- python - 如何使用 TF-ID 从 3000 个文档中找到最重要的 300 个单词?