Pavel Poley
Pavel Poley

Reputation: 5577

Rxjava2 combine multiple operations

For example if i have 3 functions

Completable requestLogin()

Single hasProjects()

Completable createDefaultProject()

How i can combine them in single request

requestLogin() > onComplete > hasProjects() > onSuccess > if(!hasProjets) > createDefaultProject()

It is possible? and what happen in case of errors?

Upvotes: 0

Views: 154

Answers (1)

Fred
Fred

Reputation: 17085

That's one of the reasons you'd use Rx. One possibility could be:

requestLogin()
   .andThen(
         hasProjects()
            .filter(value -> !value)
            .flatMapCompletable(value -> createDefaultProject()))
   .subscribe(() ->{}, throwable -> {
         // All errors will end up here
     });

We request the login and once it completes we check if there are projects. If the are not, the filter will not terminate the stream which creates the default project. If there are projects, then no default project is created.

If along the way there is any error, the onError method will be called and you can handle the errors there.

Upvotes: 2

Related Questions