rxjava序列两个请求



我有两个请求,第二个请求是在第一个请求中,因此如何按顺序进行操作,因为如果并行

请求,则有一些检查将接收到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);

您可以使用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);
        });

您不需要副作用。您可以将模型状态保存在操作员中:

@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

相关内容

  • 没有找到相关文章

最新更新