Rxjava是一个异步框架,类似于AsyncTask,其优点在于让你的异步代码更简洁,阅读代码会更轻松。知己知彼,百战不殆。
我们从源码分析Rxjava原理,可以让我们更轻松的使用Rxjava,最主要还能学到Rxjava响应式编程的思想。
先看一下简单的例子:
// 被观察者
Observable<String> observable = Observable.create(new Observable.OnSubscribe<String>() {
@Override
public void call(Subscriber<? super String> subscriber) {
Log.e(getClass().getName(), "subscriber call begin");
subscriber.onNext("value is 1");
subscriber.onNext("value is 2");
subscriber.onNext("value is 3");
subscriber.onCompleted();
}
});
// 观察者/订阅者
Observer<String> observer = new Observer<String>() {
@Override
public void onCompleted() {
Log.e(getClass().getName(), "observer is completed");
}
@Override
public void onError(Throwable e) {
Log.e(getClass().getName(), "observer is error");
}
@Override
public void onNext(String s) {
Log.e(getClass().getName(), "observer next value : " + s);
}
};
// 发生订阅关系
observable.subscribe(observer);
打印结果:
subscriber call begin
observer next value : value is 1
observer next value : value is 2
observer next value : value is 3
observer is completed
我们从被观察者开始从源码解析:
// Observable类
...
// 仅有的一个属性,OnSubscribe是一个接口
final OnSubscribe<T> onSubscribe;
// 构造方法
protected Observable(OnSubscribe<T> f) {
this.onSubscribe = f;
}
// 创建被观察者,这个方法从1.2.7版本就被弃用了,虽然被弃用了,但是我们从简单的开始分析
@Deprecated
public static <T> Observable<T> create(OnSubscribe<T> f) {
return new Observable<T>(RxJavaHooks.onCreate(f));
}
// 继承Action1的接口
public interface OnSubscribe<T> extends Action1<Subscriber<? super T>> {
// cover for generics insanity
}
// Action1接口
public interface Action1<T> extends Action {
void call(T t);
}
...
create方法只是new了一个Observable对象出来。代码提到RxJavaHooks类,我们看看这个类onCreate方法做了什么:
// RxJavaHooks类
...
static volatile Func1<Observable.OnSubscribe, Observable.OnSubscribe> onObservableCreate;
static void initCreate() {
onObservableCreate = new Func1<Observable.OnSubscribe, Observable.OnSubscribe>() {
@Override
public Observable.OnSubscribe call(Observable.OnSubscribe f) {
return RxJavaPlugins.getInstance().getObservableExecutionHook().onCreate(f);
}
};
}
public static <T> Observable.OnSubscribe<T> onCreate(Observable.OnSubscribe<T> onSubscribe) {
Func1<Observable.OnSubscribe, Observable.OnSubscribe> f = onObservableCreate;
if (f != null) {
return f.call(onSubscribe);
}
return onSubscribe;
}
...
onObservableCreate的call方法返回一个Observable.OnSubscribe实例,继续看下去
// RxJavaPlugins类,为了不影响篇幅,简洁代码
public RxJavaObservableExecutionHook getObservableExecutionHook() {
if (observableExecutionHook.get() == null) {
...
observableExecutionHook.compareAndSet(null, RxJavaObservableExecutionHookDefault.getInstance());
...
}
return observableExecutionHook.get();
}
RxJavaObservableExecutionHookDefault直接继承RxJavaObservableExecutionHook类,只是做了单例,没有做其他东西,所以我们直接看RxJavaObservableExecutionHook这个类:
// RxJavaObservableExecutionHook
@Deprecated
public <T> OnSubscribe<T> onCreate(OnSubscribe<T> f) {
return f;
}
其实就是返回本身,所以
// Observable类
public static <T> Observable<T> create(OnSubscribe<T> f) {
return new Observable<T>(RxJavaHooks.onCreate(f));
}
暂时先可以看做:
// Observable类
public static <T> Observable<T> create(OnSubscribe<T> f) {
return new Observable<T>(f);
}
这样就简单多了。我们再来看看观察者的代码:
// Observer类
public interface Observer<T> {
void onCompleted();
void onError(Throwable e);
void onNext(T t);
}
其实就是一个接口,我们继续看Observable和Observer发生订阅关系的代码:
// Observable类
public final Subscription subscribe(final Observer<? super T> observer) {
if (observer instanceof Subscriber) {
return subscribe((Subscriber<? super T>)observer);
}
if (observer == null) {
throw new NullPointerException("observer is null");
}
return subscribe(new ObserverSubscriber<T>(observer));
}
public final Subscription subscribe(Subscriber<? super T> subscriber) {
return Observable.subscribe(subscriber, this);
}
Subscriber是Observer的抽象实现类:
// 订阅者Subscriber
public abstract class Subscriber<T> implements Observer<T>, Subscription {
...
// 多了一个钩子方法
public void onStart() {
// do nothing by default
}
...
}
关于Subscriber先知道这么多,我们传进去的是Observer,而subscribe方法需要的是一个Subscriber,所以使用一个代理类ObserverSubscriber去代理Observer:
// ObserverSubscriber代理Observer
public final class ObserverSubscriber<T> extends Subscriber<T> {
final Observer<? super T> observer;
public ObserverSubscriber(Observer<? super T> observer) {
this.observer = observer;
}
@Override
public void onNext(T t) {
observer.onNext(t);
}
@Override
public void onError(Throwable e) {
observer.onError(e);
}
@Override
public void onCompleted() {
observer.onCompleted();
}
}
最后调用了Observable的静态方法subscribe开始发生订阅关系:
// Observable类
static <T> Subscription subscribe(Subscriber<? super T> subscriber, Observable<T> observable) {
if (subscriber == null) {
throw new IllegalArgumentException("subscriber can not be null");
}
if (observable.onSubscribe == null) {
throw new IllegalStateException("onSubscribe function can not be null.");
}
// 先调用start
subscriber.onStart();
if (!(subscriber instanceof SafeSubscriber)) {
subscriber = new SafeSubscriber<T>(subscriber);
}
try {
// 暂时先看做onSubscribe.call(subscriber)
// onSubscribe是我们创建Observable传进来的,subscriber就是观察者/订阅者
RxJavaHooks.onObservableStart(observable, observable.onSubscribe).call(subscriber);
return RxJavaHooks.onObservableReturn(subscriber);
} catch (Throwable e) {
...
}
}
我们再回来看例子中的Observable:
Observable<String> observable = Observable.create(new Observable.OnSubscribe<String>() {
@Override
public void call(Subscriber<? super String> subscriber) {
Log.e(getClass().getName(), "subscriber call begin");
// 直接调用观察者/订阅者的方法
subscriber.onNext("value is 1");
subscriber.onNext("value is 2");
subscriber.onNext("value is 3");
subscriber.onCompleted();
}
});
Rxjava基本流程分析到这里。