RxJava- Is cache() the same as replay()?

后端 未结 1 1797
借酒劲吻你
借酒劲吻你 2021-02-07 06:53

I was wondering if there was a cache() operator that could cache x number of emissions but also expire them after a specified time interval (e.g. 1 minute). I was l

相关标签:
1条回答
  • 2021-02-07 07:29

    cache and replay are meant for different use cases. Cache is an auto-connecting replay-everything container used typically for long-term replays. Replay can have more parametrization and can do bounded time/size replays but requires the developer to specify when to start. The autoConnect() operator lets you turn such ConnectableObservable instances to a plain Observable which connects to the source once a subscriber subscribes to them. This way, you can have a bounded and auto-connecting replay (requires RxJava 1.0.14+):

    source.replay(1, TimeUnit.SECONDS).autoConnect().subscribe(...);
    
    0 讨论(0)
提交回复
热议问题