[英]Rxjava2 combine multiple operations
For example if i have 3 functions 例如,如果我有3个功能
Completable requestLogin()
Single hasProjects()
Completable createDefaultProject()
How i can combine them in single request 我如何在单个请求中将它们组合
requestLogin() >
onComplete >
hasProjects() >
onSuccess >
if(!hasProjets) >
createDefaultProject() 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. 这就是您使用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. 如果不是,则
filter
不会终止创建默认项目的流。 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. 如果在执行过程中出现任何错误,将调用
onError
方法,您可以在那里处理错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.