Android RxJava源码分析(1)

Observable.create(new Observable.OnSubscribe() {
    @Override
    public void call(Subscriber subscriber) {
        LogUtils.e(TAG , "Looper.getLoop call: " + Thread.currentThread().getName());
        subscriber.onNext("qb");
        subscriber.onCompleted();
    }
})
        .subscribe(new Observer() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onNext(String s) {
                LogUtils.e(TAG , "Looper.getLoop onNext: " + Thread.currentThread().getName());
            }
        });Observable为被观察者,Observer为观察者,subscriber为订阅,观察者与被观察者通过订阅关联起来.接下来进行源码分析:Observable.create()方法主要是创建了一个Observable对象
public static  Observable create(OnSubscribe f) {
    return new Observable(hook.onCreate(f));
}
protected Observable(OnSubscribe f) {
    this.onSubscribe = f;
}

返回的是我们new Observable.OnSubscribe()对象。然后我们再看subscribe方法。

 

public final Subscription subscribe(final Observer observer) {
    if (observer instanceof Subscriber) {
        return subscribe((Subscriber)observer);
    }
   //走了下面这个方法
    return subscribe(new Subscriber() {

        @Override
        public void onCompleted() {
            observer.onCompleted();
        }

        @Override
        public void onError(Throwable e) {
            observer.onError(e);
        }

        @Override
        public void onNext(T t) {
            observer.onNext(t);
        }

    });
}
private static  Subscription subscribe(Subscriber subscriber, Observable observable) {

    //先调用了start()方法。
    // new Subscriber so onStart it
    subscriber.onStart();
    
    /*
     * See https://github.com/ReactiveX/RxJava/issues/216 for discussion on "Guideline 6.4: Protect calls
     * to user code from within an Observer"
     */
    // if not already wrapped
    if (!(subscriber instanceof SafeSubscriber)) {
        // assign to `observer` so we return the protected version
        //将改subscriber转成SafeSubscriber对象,该对象内部还是继承了Subscriber类
        subscriber = new SafeSubscriber(subscriber);
    }


    try {
        // allow the hook to intercept and/or decorate
        //主要是执行了这个方法,这个方法 hook.onSubscribeStart(observable, observable.onSubscribe)返回的是我们之前new Observable.OnSubscribe()对象,然后该对象对用了call方法,即需要我们自己实现的call()方法。
        hook.onSubscribeStart(observable, observable.onSubscribe).call(subscriber);
        return hook.onSubscribeReturn(subscriber);
    } catch (Throwable e) {
        // special handling for certain Throwable/Error/Exception types
        Exceptions.throwIfFatal(e);
        // if an unhandled error occurs executing the onSubscribe we will propagate it
        try {
            subscriber.onError(hook.onSubscribeError(e));
        } catch (Throwable e2) {
            Exceptions.throwIfFatal(e2);
            // if this happens it means the onError itself failed (perhaps an invalid function implementation)
            // so we are unable to propagate the error correctly and will just throw
            RuntimeException r = new RuntimeException("Error occurred attempting to subscribe [" + e.getMessage() + "] and then again while trying to pass to onError.", e2);
            // TODO could the hook be the cause of the error in the on error handling.
            hook.onSubscribeError(r);
            // TODO why aren't we throwing the hook's return value.
            throw r;
        }
        return Subscriptions.unsubscribed();
    }
}

然后我们调用subscriber.onNext("qb");

subscriber.onCompleted();实现完成。

 

你可能感兴趣的:(源码分析)