RxJava 中 doOnNext(...) 的目的是什么
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28402689/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the purpose of doOnNext(...) in RxJava
提问by Aravind Yarram
When should we use doOnNext()from Observable instead of just onNext()?
我们什么时候应该使用doOnNext()可观察的,而不是仅仅onNext()?
采纳答案by Simon Baslé
doOnNext
is for side-effects: you want to react (eg. log) to item emissions in an intermediate step of your stream, for example before the stream is filtered, for transverse behavior like logging, but you still want the value to propagate down the stream.
doOnNext
是为了副作用:您想对流的中间步骤中的项目排放做出反应(例如记录),例如在过滤流之前,对于记录等横向行为,但您仍然希望该值向下传播溪流。
onNext
is more final, it consumes the value.
onNext
更最终,它消耗了价值。
回答by trocchietto
IMPORTANT EDIT: -in bold characters immediately below-
重要编辑: - 在下面的粗体字符中 -
* Once one grasps the concept,
I warmly suggest you to have a look at this link, is a lifechanger, not only because we use different observable as Observable
, Single
, Maybe
that could be need a different tool as doOnEvent()
for Single
and doOnEach()
for Observable
but because if you want to debug, there are some reasons why doOnNext() could often even not be the ideal choice, because we could ignore other events that are relevant to resolve the problem*
*一旦一个掌握这个概念,我热烈建议你看看这个链接,是一个lifechanger,不仅是因为我们使用不同的观察为 Observable
,Single
,Maybe
可能被需要不同的工具,doOnEvent()
用于Single
与doOnEach()
供Observable
,而是因为,如果你想调试,有一些原因为什么 doOnNext() 通常甚至不是理想的选择,因为我们可以忽略与解决问题相关的其他事件*
ORIGINAL REPLY: -partially modified-
原始回复:-部分修改-
First of all doOnNext()
can be called even more timesin the chain of operators between Observable and Subscribe, this gives to you greater possibilities to debug your code. Because of its "stream" nature, is not easy to do debugging in RXJava, doOnNext()
instead makes debugging easier. At this purpose you may consider also to combine it with thedoOnError()
operator. Why not using a simple onNext()
? Because debugging is not strictly related to the logic of the code, in theory you could also eliminate the doOnNext()
before to go in production.
首先doOnNext()
可以在 Observable 和 Subscribe 之间的运算符链中调用更多次,这为您调试代码提供了更大的可能性。由于其“流”性质,在 RXJava 中doOnNext()
进行调试并不容易,反而使调试更容易。为此,您还可以考虑将其与doOnError()
运算符结合使用。为什么不使用简单的onNext()
?因为调试与代码的逻辑没有严格的关系,理论上你也可以去掉doOnNext()
之前的去生产。
A really essential thing to understand is that given an Observable to subscribe long chain, you can use the doOnNext in a particular point, to see what an operator is giving back to another one:
需要理解的一个非常重要的事情是,给定一个 Observable 订阅长链,您可以在特定点使用 doOnNext 来查看操作员返回给另一个操作员的内容:
For instance :
例如 :
Observable.just("Donald", "Duck", "Mickey", "Goofy",
"Uncle")
.doOnNext{System.out.println("Here ou will get the strings above:$it ")}
.map{it.length}
.subscribe { println("Here you will get the numbers of how every string is long: $it") }}
A typical use case to employ doOnNext()
could for example occur when you want to cache a response from a server, so for instance you could use map()
but alsodoOnNext()
, because it allows you to make your code more readable as instead you would put a simple onNext()
that ideally could have been structured to follow other instructions. (This is arguable, as all the architectural thoughts)
doOnNext()
例如,当您想要缓存来自服务器的响应时,可能会使用一个典型的用例,因此例如您可以使用map()
但也doOnNext()
,因为它允许您使您的代码更具可读性,而不是您将一个简单的onNext()
,理想地可以已被构造为遵循其他说明。(这是有争议的,因为所有的建筑思想)
Equally to doOnNext()
and for the same debugging purposes
you could use others self-explanatory operators:
同样doOnNext()
出于相同的调试目的,您可以使用其他不言自明的运算符:
doOnSubscribe(), doOnUnsubscribe(), doOnCompleted(), doOnError(), doOnTerminate(),finallyDo(), doOnEach(), doOnRequest()
doOnSubscribe()、doOnUnsubscribe()、doOnCompleted()、doOnError()、doOnTerminate()、finallyDo()、doOnEach()、doOnRequest()
doOnNext()
allow you to see what is going on into the Observable (often really long) chains, what is really important is that you can kinda spying what is going on trough the chain, without affecting any operation, without making any transformation( let's say not properly a kind of Log.d
we use in imperative, not reactive code).This is is the reason why is called a sideeffect.
doOnNext()
允许您查看 Observable(通常很长)链中发生的事情,真正重要的是您可以通过链窥探正在发生的事情,而不影响任何操作,不进行任何转换(假设不正确)Log.d
我们在命令式而非反应式代码中使用的一种)。这就是为什么被称为副作用的原因。
EDIT(because of the question in the comments):
编辑(因为评论中的问题):
doOnNext()
and the method above are just callbacks, please refer to that, doOnNext()
as the official documentation says
doOnNext()
和上面的方法都只是回调,请参阅该,doOnNext()
作为官方文件说,
just modifies an Observable so that it invokes an action when it calls onNext.
只是修改一个 Observable 以便它在调用 onNext 时调用一个动作。
Really simple, this is why is called sometimes to upload the progress bar, but also is really used in the repository pattern, for instance if you want to store data to the db/or caching after a call to retrofit for instance.
真的很简单,这就是为什么有时会调用上传进度条,但也确实用于存储库模式,例如如果您想将数据存储到数据库/或在调用改造后缓存。
If you are really curious under the hood the doSomethingReactive methods just call the method call()
( a callback from the Interface Action) inside the "real" method SomethingReactive
如果您真的很好奇,那么 doSomethingReactive 方法只需调用call()
“真实”方法SomethingReactive 中的方法(来自接口操作的回调)