RxAndroid与RxJava 变换的原理值lift()

lift的中文意思的电梯,实质是针对事件序列的处理和再发送,在RxJava的内部,在Rxjava的内部,他们都是基于同一个基础的变换方法lift()

源码:

    public final <R> Observable<R> lift(final Operator<? extends R, ? super T> operator) {
        return new Observable<R>(new OnSubscribe<R>() {
            @Override
            public void call(Subscriber<? super R> o) {
                try {
                    Subscriber<? super T> st = hook.onLift(operator).call(o);
                    try {
                        // new Subscriber created and being subscribed with so 'onStart' it
                        st.onStart();
                        onSubscribe.call(st);
                    } catch (Throwable e) {
                        // localized capture of errors rather than it skipping all operators 
                        // and ending up in the try/catch of the subscribe method which then
                        // prevents onErrorResumeNext and other similar approaches to error handling
                        Exceptions.throwIfFatal(e);
                        st.onError(e);
                    }
                } catch (Throwable e) {
                    Exceptions.throwIfFatal(e);
                    // if the lift function failed all we can do is pass the error to the final Subscriber
                    // as we don't have the operator available to us
                    o.onError(e);
                }
            }
        });
    }

这里的大概意思是:创建了一个新的Observable并返回,而创建新的Observable所用的参数OnSubscribe中的call方法的有点不一样,使用的新的Observable的OnSubcribe

实际意思:在Observable执行lift(Operator)方法之后,返回了一个新的Observable,这个Observable会像代理一样,负责接收原始的Observable发出的事件,并在处理后发送Subscribe

RxAndroid与RxJava 变换的原理值lift()_第1张图片

但是官方好像不建议使用lift,而是建议使用lift包装的方法map,flatmap来实现具体的逻辑



你可能感兴趣的:(RxAndroid与RxJava 变换的原理值lift())