首页 > 解决方案 > 链两个答案单

问题描述

我需要链接两个 RX Single 响应 - 改造以获取 ArrayList 与返回 List 的两个响应

我试图用 , 处理这两个答案MapFlatmap但我没有达到我的预期

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 中包含两个响应的所有对象,以便稍后绘制它们。

标签: javaandroidretrofitrx-javareactive

解决方案


你有两种方法可以做到这一点。

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();

推荐阅读