ferpar1988
ferpar1988

Reputation: 606

Rxjava cache operator

I want to cache observable items for subsequent subscriptions but I don't want to cache errors. It seems cache operator also caches throwables. How can I achive that?

Upvotes: 3

Views: 2406

Answers (1)

Dan Lew
Dan Lew

Reputation: 87420

There's no way to make cache itself stop caching terminal events (onError and onCompleted). But you can filter out the terminal events before they occur.

I wrote about some ways to handle errors in a post here. Basically, you can use one of the catch operators like onErrorReturn() or onErrorResumeNext() to convert those errors into non-errors.

Alternatively, if you could use materialize() + dematerialize() and filter out any error notifications. But functionally that isn't different from using onErrorResumeNext() with Observable.empty().


As an example, you'd basically do something like this:

observable
  .onErrorResumeNext(throwable -> Observable.empty())
  .cache()

This would filter out errors but then cache the rest.

Upvotes: 6

Related Questions