连锁两个答案单

时间:2018-12-20 16:51:02

标签: java android retrofit rx-java reactive

我需要链接两个RX Single响应-进行改造以获取具有返回列表的两个响应的ArrayList

我尝试用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中,以便稍后进行绘制。

2 个答案:

答案 0 :(得分:0)

您有2种方法可以做到这一点。

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

答案 1 :(得分:0)

如果您的订单计价,我建议您将.concat作为@ebasha响应,但如果您的订单不计价,我建议您使用.merge becase比.concat快得多,因为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());

Observable.merge(responseDspConsent.toObservable(),responseDspConsentByApp.toObservable())
                   .toList()
                   .doOnSuccess((list) -> {
                       paintAllConsents(list); 
                   })
                   .subscribe();