java rx中Observable.defer和Observable.create的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36313946/
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
Difference between Observable.defer and Observable.create in java rx
提问by Xitrum
Can someone explain me the difference between defer
and create
methods in Observable
? I failed to understand when I should use defer
and when should I use create
..
有人可以解释一下defer
和 中的create
方法之间的区别Observable
吗?我不明白什么时候应该使用defer
,什么时候应该使用create
..
REFERENCES:
参考:
Defer: http://reactivex.io/documentation/operators/defer.html
推迟:http: //reactivex.io/documentation/operators/defer.html
Create: http://reactivex.io/documentation/operators/create.html
创建:http: //reactivex.io/documentation/operators/create.html
Thank you
谢谢
回答by Bob Spryn
So the distinction seems to be: defer
is good when you have something that creates/returns an observable already, but you don't want it to that process to happen until subscription.
所以区别似乎是:defer
当你有一些已经创建/返回一个可观察的东西时很好,但你不希望它在订阅之前发生在那个过程中。
create
is good when you need to manually wrap an async process and create an observable. That creation is alsodeferred until subscription.
create
当您需要手动包装异步进程并创建可观察对象时,这很好。该创建也被推迟到订阅。
To put it another way:
换一种方式:
defer
is an operator that enables deferred composition of observable sequences.
defer
是一个操作符,可以实现可观察序列的延迟组合。
create
is a custom implementation of observable sequence (where creation is deferred until subscription).
create
是可观察序列的自定义实现(其中创建被推迟到订阅)。
So if you have a situation where you might use just
to create an Observable
from some results/value or you have a network API layer that returns an Observable
of the request, but you don't want that request to kick off until subscription. defer
would be good for those scenarios.
因此,如果您遇到可能用于从某些结果/值just
创建一个的情况,Observable
或者您有一个返回Observable
请求的一个网络 API 层,但您不希望该请求在订阅之前开始。defer
对那些场景会有好处。
If you have a network API layer that doesn'treturn an Observable
for a request, but which you need an Observable
interface to, you might use create
. That Observable
sequence still wouldn't be created until subscription though. If you wanted that network call to kick off regardless of subscription, then you would use a different mechanism, like a Subject
, potentially that replays.
如果您的网络 API 层不Observable
为请求返回,但您需要一个Observable
接口,则可以使用create
. 尽管如此,该Observable
序列仍然不会在订阅之前创建。如果您希望无论订阅如何都开始该网络调用,那么您将使用不同的机制,例如Subject
,可能会重播。
回答by Dennis R
create(...) actually creates Observable immediately.
create(...) 实际上立即创建了 Observable。
public final static <T> Observable<T> create(OnSubscribe<T> f) {
return new Observable<T>(hook.onCreate(f));
}
defer(...) accepts Factory function that returns Observable(Subject, etc...), wraps it with OnSubscribeDefer and creates Observable only when subscriber subscribes, new Observable for every subscriber.
defer(...) 接受返回 Observable(Subject, etc...) 的工厂函数,用 OnSubscribeDefer 包装它并仅在订阅者订阅时创建 Observable,为每个订阅者创建新的 Observable。
public final static <T> Observable<T> defer(Func0<Observable<T>> observableFactory) {
return create(new OnSubscribeDefer<T>(observableFactory));
}
See some more details here
在此处查看更多详细信息