RxJava按顺序请求两个

时间:2017-04-27 16:31:28

标签: java android rx-java system.reactive rx-android

我有两个请求,第二个是它依赖于First,所以如何按顺序进行,因为有一些检查会在并行请求时收到null

        Observable<Map<Integer, SupportedVersion>> supportedVersionObservable = contentAPI
            .getSupportedVersionsContent()
            .compose(ReactiveUtils.applySchedulers())
            .map(supportedVersionsContentContentContainer -> supportedVersionsContentContentContainer.getContent().get(0).getMessage())
            .doOnNext(supportedVersionsMap -> {
                Timber.i("doOnNext invoked from supported version observable");
                for (Map.Entry<Integer,SupportedVersion> entry : supportedVersionsMap.entrySet())
                    if (Build.VERSION.SDK_INT >= entry.getKey())
                        model.setSupportedVersion(entry.getValue());

                model.setCurrentVersionExpiryDate(model.getSupportedVersion().getCurrentVersionExpiryDate());

                if (model.getSupportedVersion() != null)
                    model.setNewFeaturesSeen(sharedPreferencesManager.isNewFeaturesSeen(model.getSupportedVersion().getAvailableVersions().get(0)));
                if (model.isNewFeaturesSeen());
                //request data from here 
            })
            .retry(1);

    Observable<List<WhatsNew>> getWhatsNewFeature = contentAPI
            .getWhatsNewFeature(model.getSupportedVersion().getAvailableVersions().get(0))
            .compose(ReactiveUtils.applySchedulers())
            .doOnNext(whatsNewList -> {
                Timber.i("doOnNext invoked from supported version observable");
                if (!whatsNewList.isEmpty())
                    model.setWhatsNews(whatsNewList);
            })
            .retry(1);

2 个答案:

答案 0 :(得分:3)

您可以使用flatMap:

public Observable<List<WhatsNew>> makeRequest { 
   return contentAPI
        .getSupportedVersionsContent()
        .flatMap(supportedVersionsMap -> {
             //... model initialization
             return contentAPI
                    .getWhatsNewFeature(model.getSupportedVersion().getAvailableVersions().get(0))
                    .compose(ReactiveUtils.applySchedulers())
                    .doOnNext(whatsNewList -> {
                    Timber.i("doOnNext invoked from supported version observable");
                    if (!whatsNewList.isEmpty())
                        model.setWhatsNews(whatsNewList);
                    })
                   .retry(1);
        });

答案 1 :(得分:1)

您不需要副作用。您可以在运算符中保存模型状态:

@Test
void name() {
    ContentApi mock = mock(ContentApi.class);

    Observable<Model> modelObservable = mock.getSupportedVersionsContent()
            .map(s -> {
                // do Mapping
                return new Model();
            })
            .flatMap(model -> mock.getWhatsNewFeature(model)
                    .map(whatsNews -> {
                        // Create new model with whatsNews
                        return new Model();
                    }), 1);
}

interface ContentApi {
    Observable<String> getSupportedVersionsContent();

    Observable<List<WhatsNew>> getWhatsNewFeature(Model model);
}

class Model {

}

class WhatsNew {

}

请查看flatMap的详细说明:

http://tomstechnicalblog.blogspot.de/2015/11/rxjava-operators-flatmap.html?m=0