简体   繁体   中英

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM