How to ignore error and continue infinite stream?

后端 未结 10 1755
执念已碎
执念已碎 2020-12-05 06:18

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

10条回答
  •  南方客
    南方客 (楼主)
    2020-12-05 07:01

    Just pasting the link info from @MikeN's answer incase it gets lost:

    import rx.Observable.Operator;
    import rx.functions.Action1;
    
    public final class OperatorSuppressError implements Operator {
        final Action1 onError;
    
        public OperatorSuppressError(Action1 onError) {
            this.onError = onError;
        }
    
        @Override
        public Subscriber call(final Subscriber t1) {
            return new Subscriber(t1) {
    
                @Override
                public void onNext(T t) {
                    t1.onNext(t);
                }
    
                @Override
                public void onError(Throwable e) {
                    onError.call(e);
                }
    
                @Override
                public void onCompleted() {
                    t1.onCompleted();
                }
    
            };
        }
    }
    

    and use it close to the observable source because other operators may eagerly unsubscribe before that.

    Observerable.create(connectToUnboundedStream()).lift(new OperatorSuppressError(log()).doOnNext(someStuff()).subscribe();
    

    Note, however, that this suppresses the error delivery from the source. If any onNext in the chain after it throws an exception, it is still likely the source will be unsubscribed.

提交回复
热议问题