【问题标题】:make Observable optional in zip operator with retrofit通过改造使 Observable 在 zip 运算符中成为可选
【发布时间】:2017-01-25 18:55:47
【问题描述】:

我有一个经过改造的 Observable 调用,它压缩了三个 API 调用 但我想同时进行 3 个调用,但有时其中一个调用失败,但我只有一个主调用,这对我来说是强制性的,其余调用是可选的,因为当其中一个调用失败时,它会出现错误,而我不希望那样,我在想是否有像 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();

【问题讨论】:

    标签: android retrofit rx-java rx-android


    【解决方案1】:

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

    对于可选源,请使用onErrorResumeNextonErrorReturnItemonErrorReturn 运算符之一。您可以将错误替换为可以成功zipped的虚拟值:

    Observable.zip(
        source1,
        source2,
        optionalSource3.onErrorReturnItem(stub)
    )
    ...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-09-07
      • 2017-09-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-19
      相关资源
      最近更新 更多