通过改造使zip运算符中的Observable可选

时间:2017-01-25 09:49:51

标签: android retrofit rx-java rx-android

我有一个带有改造的Observable调用,它压缩了三个API调用 但我希望将3个调用放在一起,但有时其中一个调用失败,但我只有一个主调用,这对我来说是强制性的,其余的调用是可选的,因为当其中一个调用失败时,它会执行Error,而我我不想这样,我在想是否有JoinObservable.when(OperatorJoinPatterns.or(call1,call2).then 但唯一的是和

    Observable.zip(getSearchObservable(FIRST_PAGE), App.getApi().allbookmarks(), SpotlightUtil.getSpotLightBanner(), App.getApi().getFollowingSuggestions(AppConfigUtil.getFollowingSuggestions().getLimit()),
                (searchResult, myFavouritesResults, spotlightListResult, followingSuggestionsResult) -> combineCall(searchResult, myFavouritesResults, spotlightListResult, followingSuggestionsResult, false))
                .observeOn(AndroidSchedulers.mainThread())
                .doOnNext(spotlightsAndSearchResultAndSuggestionsResult -> {
//my main call that i want if that fail the request should fail 
                    if (!NetUtils.isServerOk(spotlightsAndSearchResultAndSuggestionsResult.getSearchResult().getStatus())) {
                        throw new ServerErrorException(spotlightsAndSearchResultAndSuggestionsResult.getSearchResult().getErrorMessage());
                    }

                    if (spotlightsAndSearchResultAndSuggestionsResult.getSearchResult().posts.size() < PAGE_SIZE) {
                        rvPosts.setFinished(true);
                    }
                    hideLoader();
                    mPostAdapter.mSuggestions = spotlightsAndSearchResultAndSuggestionsResult.getFollowingSuggestionsResult().getSuggestion();
                    checkToAddOrRemoveFeedbackSpotLight(spotlightsAndSearchResultAndSuggestionsResult.getSearchResult().posts, true);
                                    })
                .doOnError(throwable -> {
                    ErrorScreenUtils.checkError(throwable, this, true);
                    hideLoader();
                })
                .retryWhen(RxActivity.RETRY_CONDITION).compose(bindUntilEvent(FragmentEvent.DESTROY))
                .subscribe();

1 个答案:

答案 0 :(得分:1)

doOnError不会阻止错误传播,因此会破坏您的逻辑。

对于可选来源,请使用onErrorResumeNextonErrorReturnItemonErrorReturn运算符之一。您可以使用可以成功{false}成功的虚拟值替换错误:{/ 1}}:

zip