How to ignore error and continue infinite stream?

后端 未结 10 1699
执念已碎
执念已碎 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:00

    A slight modification of the solution (@MikeN) to enable finite streams to complete:

    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);
                    //this will allow finite streams to complete
                    t1.onCompleted();
                }
    
                @Override
                public void onCompleted() {
                    t1.onCompleted();
                }
    
            };
        }
    }
    

提交回复
热议问题