I would like to know how to ignore exceptions and continue infinite stream (in my case stream of locations)?
I\'m fetching current user position (using Android-React
Here is my kotlin extension function for ignoring errors
fun <T> Observable<T>.ignoreErrors(errorHandler: (Throwable) -> Unit) =
retryWhen { errors ->
errors
.doOnNext { errorHandler(it) }
.map { 0 }
}
This utilizes retryWhen
to indefinately re-subscribe to the upstream while still allowing you a method of handling the error in a non terminal way.
This feels dangerous
This answer might be a bit late, but if anyone stumbles upon this, instead of reinventing a wheel can use the ready to use Relay lib by Jacke Wharton
https://github.com/JakeWharton/RxRelay
there is good documentation but in essence, Relay is A Subject except without the ability to call onComplete or onError.
and the options are:
BehaviorRelay
Relay that emits the most recent item it has observed and all subsequent observed items to each subscribed Observer.
// observer will receive all events.
BehaviorRelay<Object> relay = BehaviorRelay.createDefault("default");
relay.subscribe(observer);
relay.accept("one");
relay.accept("two");
relay.accept("three");
// observer will receive the "one", "two" and "three" events, but not "zero"
BehaviorRelay<Object> relay = BehaviorRelay.createDefault("default");
relay.accept("zero");
relay.accept("one");
relay.subscribe(observer);
relay.accept("two");
relay.accept("three");
PublishRelay Relay that, once an Observer has subscribed, emits all subsequently observed items to the subscriber.
PublishRelay<Object> relay = PublishRelay.create();
// observer1 will receive all events
relay.subscribe(observer1);
relay.accept("one");
relay.accept("two");
// observer2 will only receive "three"
relay.subscribe(observer2);
relay.accept("three");
ReplayRelay Relay that buffers all items it observes and replays them to any Observer that subscribes.
ReplayRelay<Object> relay = ReplayRelay.create();
relay.accept("one");
relay.accept("two");
relay.accept("three");
// both of the following will get the events from above
relay.subscribe(observer1);
relay.subscribe(observer2);
With Rxjava2, we could call the overloaded flatmap with delayErrors parameter: flatmap javadoc
When passing it as true:
exceptions from the current Flowable and all inner Publishers are delayed until all of them terminate if false, the first one signaling an exception will terminate the whole sequence immediately
You may want to use one of the error handling operators.
onErrorResumeNext( )
— instructs an Observable to emit a sequence of items if it encounters an erroronErrorReturn( )
— instructs an Observable to emit a particular item when it encounters an erroronExceptionResumeNext( )
— instructs an Observable to continue emitting items after it encounters an exception (but not another variety of throwable)retry( )
— if a source Observable emits an error, resubscribe to it in the hopes that it will complete without errorretryWhen( )
— if a source Observable emits an error, pass that error to another Observable to determine whether to resubscribe to the sourceEspecialy retry
and onExceptionResumeNext
look promising in your case.