Single (RxJava Javadoc 1.3.8) 您所在的位置:网站首页 Single Single (RxJava Javadoc 1.3.8)

Single (RxJava Javadoc 1.3.8)

2022-06-06 22:25| 来源: 网络整理| 查看: 265

Returns a Single that subscribes to this Single lazily, caches its success or error event and replays it to all the downstream subscribers.

This is useful when you want a Single to cache its response and you can't control the subscribe/unsubscribe behavior of all the Subscribers.

The operator subscribes only when the first downstream subscriber subscribes and maintains a single subscription towards this Single. In contrast, the operator family of Observable.replay() that return a ConnectableObservable require an explicit call to ConnectableObservable.connect().

Note: You sacrifice the ability to unsubscribe from the origin when you use the cache Observer so be careful not to use this Observer on Observables that emit an infinite or very large number of items that will use up memory. A possible workaround is to apply `takeUntil` with a predicate or another source before (and perhaps after) the application of cache().

AtomicBoolean shouldStop = new AtomicBoolean(); source.takeUntil(v -> shouldStop.get()) .cache() .takeUntil(v -> shouldStop.get()) .subscribe(...); Since the operator doesn't allow clearing the cached values either, the possible workaround is to forget all references to it via Observable.onTerminateDetach() applied along with the previous workaround: AtomicBoolean shouldStop = new AtomicBoolean(); source.takeUntil(v -> shouldStop.get()) .onTerminateDetach() .cache() .takeUntil(v -> shouldStop.get()) .onTerminateDetach() .subscribe(...); Backpressure: The operator consumes this Single in an unbounded fashion but respects the backpressure of each downstream Subscriber individually. Scheduler: cache does not operate by default on a particular Scheduler.


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有