通过改造使 Observable 在 zip 运算符中可选
make Observable optional in zip operator with retrofit
我有一个带有改造的 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();
doOnError
不会阻止错误传播,因此它会破坏您的逻辑。
对于可选源,使用 onErrorResumeNext
、onErrorReturnItem
、onErrorReturn
运算符之一。您可以用可以成功 zip
ped:
的虚拟值替换错误
Observable.zip(
source1,
source2,
optionalSource3.onErrorReturnItem(stub)
)
...
我有一个带有改造的 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();
doOnError
不会阻止错误传播,因此它会破坏您的逻辑。
对于可选源,使用 onErrorResumeNext
、onErrorReturnItem
、onErrorReturn
运算符之一。您可以用可以成功 zip
ped:
Observable.zip(
source1,
source2,
optionalSource3.onErrorReturnItem(stub)
)
...