RxJava if/else with multiple chain calls - android

I have a webservice call that return an object in which there is a parameter that indicates whether the operation ended successfully or not, so I would like to filter it (kind of if/else statement) inside the RxJava chain by using RxJava operators. Is it possible?
Something like this but not using if/else:
repo.webserviceCall(username, password)
.flatMap(result -> {
if (result.isSuccessful())
repo.secondWebserviceCall(result.getInfo())
else
showToastMessage("Api call not successful"); //STOP FLOW HERE
})
.flatMap(result -> thirdWebserviceCall(res))
.subscribe(res -> {showSuccessMssg(res)}, throwable -> { showError(t)});

You can return an error() from your flatMap so that the execution then goes to the onError consumer in your subscribe call.

If each service call returns one item, you could rearrange the operators so that not successful won't run the flatMap for the second and third calls. The filter will turn the setup to empty for which you can use the onComplete handler to display the toast.
repo.webserviceCall(username, password)
.filter(result -> result.isSuccessful())
.flatMap(result ->
repo.secondWebserviceCall(result.getInfo())
.flatMap(result -> thirdWebserviceCall(res))
)
.subscribe(
res -> showSuccessMssg(res),
throwable -> showError(t),
() -> showToastMessage("Api call not successful")
);

Related

Only the original thread that created a view hierarchy can touch its views when chaining Observables

I am trying to chain two network calls in my Android app. I am using Retrofit. Basically I want to do :
Make API Call to login
Wait for the response of login, save the token to SharedPrefs
Make another API call right after I've saved the token
Wait for the response, save the data
I think I have chained the stream in the right way, the only thing is I want to update the UI in between. For example once the call starts I want to display a progressDialog ( I do that in doOnSubscribe ), or dismiss the Dialog once the call has completed ( I do that in doOnComplete ). However I get the exception Only the original thread that created a view hierarchy can touch its views. I subscribe on the io thread and observe on the mainThread so that I can make the changes to the UI, however I must be missing something.
I tried adding .subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
lower in the stream, but I still get the same error message.
getView().onLoginAction().subscribe(aVoid -> Observable.combineLatest(
getView().userNameObservable().map(CharSequence::toString),
getView().passwordObservable().map(CharSequence::toString),
Pair::new)
.first()
.subscribe(usernamePasswordPair -> {
User user = User.create(usernamePasswordPair.first, usernamePasswordPair.second, "");
RetrofitClientInstance.createService(AuthenticationNetworkApi.class).login(new Login(user.username(), user.password()))
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.doOnNext(loginResponse -> {
AuthorizationResponse responseBody = loginResponse.body();
if (responseBody != null && responseBody.getAccessToken() != null && !responseBody.getAccessToken().isEmpty()) {
if (localStorage.getAccessToken().isEmpty()) {
localStorage.saveAccessToken(responseBody.getAccessToken());
}
}
}
).
doOnSubscribe( action -> getView().showProgressDialog())
.doOnError(error -> {
getView().dismissProgressDialog();
getView().showErrorMessage("Login Unsuccessful");
}).doOnComplete(() -> getView().dismissProgressDialog()
)
.flatMap(response -> RetrofitClientInstance.createService(ActivitiesApi.class).getUserActivities())
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.doOnNext(activities -> {
for (UserActivityApiModel useractivity : activities
) {
activityService.addActivity(Activity.create(Integer.parseInt(useractivity.getId()), useractivity.getActivityName(), useractivity.getDate(),
Integer.parseInt(useractivity.getValue()), Integer.parseInt(useractivity.getSubCategory().getId())));
}
}).doOnError(error -> getView().showErrorMessage(error.getMessage()))
.doOnComplete(() -> getView().redirectToHomeScreen())
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe();
}));
The error occurs here :
.doOnError(error -> {
getView().dismissProgressDialog();
getView().showErrorMessage("Login Unsuccessful");
})
It seems you are using a different thread to execute your backend. In that case, you can't touch the main UI thread from the second one. You need to execute first runOnUiThread { //your code }
In //your code, call the two lines of code that you put on doOnError.

How to add the body of the subscribe method

In the below code, I am trying to add the body for the .subscribe(). I tried to add the lambda notation but it never worked. Would you please tell me how to implement the .subscribe() method?
Given that, the setupCommRequestService() returns Single<..>
code:
setupCommRequestService()?.
flatMap {
it.getAllPhotos()
.map {
Observable.fromIterable(it)
.map {
it
}
}
.toSortedList()
}
?.subscribeOn(Schedulers.io())
?.observeOn(AndroidSchedulers.mainThread())
?.subscribe(
)
There are 4 implementations for subscribe method according Single documentation. In a simple approach, you should implement a strategy for both onSucess and onError. therefor you should use the subscribe method either by passing a BiConsumer or 2 Consumer one for onSucess case and one for onError.
using BiConsumer in lambda:
val disposable = Single.just(1)
.subscribe { success, failure ->
/* whichever is not null */
}
or using 2 Consumer in lambda:
val disposable = Single.just(1)
.subscribe({ success ->
/* success */
}, { failure ->
/* failure */
})

RxJava2 subscribe is not called when mapping observable to completable

I have a click event which needs to make a network request.
RxView.clicks(button)
.flatMapCompletable({ x -> networkCall() })
.subscribe(...)
The click is an Observable.
networkCall returns a Completable.
However the block inside subscribe is never called when i tap the button.
I've also tried
RxView.clicks(button)
.flatMap({ x -> networkCall().toObservable<Void>() })
.subscribe(...)
How can I get this to work so that each time I tap on the button, a network request is made and is then handled in the subscribe.
EDIT:
I haven't done the network stuff yet so currently it's just
public Completable networkCall() {
Completable.complete();
}
So it's guaranteed to complete.
The flatMap case needs items, otherwise its onComplete will never fire due to the already mentioned never-completing clicks source. For example:
RxView.clicks(button)
.flatMap({ x -> networkCall().andThen(Observable.just("irrelevant")) })
.observeOn(AndroidSchedulers.mainThread())
.subscribe({ v -> System.out.println(v)}, { e -> e.printStackTrace() })

make Observable optional in zip operator with retrofit

I have a Observable call with retrofit that zipped three API calls
but I want to have the 3 calls together , but sometime one of the calls fails but I only have one main call which is mandatory for me and the rest of calls is optional , because when one of them fails it do on Error , and I don't want that, I was thinking if there is like JoinObservable.when(OperatorJoinPatterns.or(call1 , call2 ) .then
but the only thing is and
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 does not stop error propagation, so it breaks your logic.
For optional source use one of onErrorResumeNext, onErrorReturnItem, onErrorReturn operators. You can replace error with dummy value that can be successfully zipped:
Observable.zip(
source1,
source2,
optionalSource3.onErrorReturnItem(stub)
)
...

Retrofit call within Stream

I have an edit text which allows a user to input a username and once the username is input the value is sent to the db to check whether the username already exists,if not then further operations are allowed else an error is displayed.
As of now this is my current code.
usernameObservable
.skip(2)
.debounce(800, TimeUnit.MILLISECONDS)
.subscribe(username -> {
Observable<Boolean> observable = apiService.isAvailable(username);
observable.observeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(aBoolean -> {
}, throwable -> {
});
});
For now the network request is being made at the end but is it possible to make the request before and once i receive data i perform some other operations on the stream.
You are looking for flatMap operator. It allows you to transform an event into another observable, which will forward emissions to the original stream. You error notification will be forwarded as well.
usernameObservable
.skip(2)
.debounce(800, TimeUnit.MILLISECONDS)
.flatMap(username -> apiService.isAvailable(username))
.subscribe(isAvailableResult -> {
// react here
}, throwable -> {
// show an error here
});

Categories

Resources