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();
}
});
Observable<Integer> mapObservable = observable.map(new Func1<String, Integer>() {
@Override
public Integer call(String s) {
return Integer.parseInt(s.substring(s.length() - 1));
}
});
Observer<Integer> observer = new Observer<Integer>() {
@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(Integer s) {
Log.e(getClass().getName(), "observer next value : " + s);
}
};
mapObservable.subscribe(observer);
从代码可以看出,map方法的主要作用是:
Observable<T> ----------------> Observable<R>
我们看看Observable的map方法怎么做到的:
// Observable类
public final <R> Observable<R> map(Func1<? super T, ? extends R> func) {
return unsafeCreate(new OnSubscribeMap<T, R>(this, func));
}
public static <T> Observable<T> unsafeCreate(OnSubscribe<T> f) {
return new Observable<T>(RxJavaHooks.onCreate(f));
}
先创建一个OnSubscribeMap作为新的OnSubscribe(OnSubscribe2),返回新创建Observable(Observable2,也就是例子中的mapObservable ),当Observable与Observer发生订阅关系时,OnSubscribe2的call方法会被调用,所以我们看看OnSubscribeMap的call方法:
// OnSubscribeMap类
public final class OnSubscribeMap<T, R> implements OnSubscribe<R> {
final Observable<T> source;
final Func1<? super T, ? extends R> transformer;
public OnSubscribeMap(Observable<T> source, Func1<? super T, ? extends R> transformer) {
this.source = source;
this.transformer = transformer;
}
@Override
public void call(final Subscriber<? super R> o) {
MapSubscriber<T, R> parent = new MapSubscriber<T, R>(o, transformer);
o.add(parent);
source.unsafeSubscribe(parent);
}
}
先由以前的观察者/订阅者(也就是例子中的observer)生成一个新的观察者/订阅者(MapSubscriber),我们简单看一下MapSubscriber类:
// MapSubscriber类
static final class MapSubscriber<T, R> extends Subscriber<T> {
final Subscriber<? super R> actual;
final Func1<? super T, ? extends R> mapper;
boolean done;
public MapSubscriber(Subscriber<? super R> actual, Func1<? super T, ? extends R> mapper) {
this.actual = actual;
this.mapper = mapper;
}
@Override
public void onNext(T t) {
...
}
@Override
public void onError(Throwable e) {
...
}
@Override
public void onCompleted() {
...
}
@Override
public void setProducer(Producer p) {
actual.setProducer(p);
}
}
然后让之前的Observable(也就是例子中的原始的observable)和新的观察者/订阅者(MapSubscriber)发生订阅关系,那么原来的OnSubscribe的call方法便会执行,记住原来的OnSubscribe的call方法的参数subscriber是新的观察者/订阅者(MapSubscriber),所以在call方法调用subscriber的onNext方法:
subscriber.onNext("value is 1");
新的观察者/订阅者(MapSubscriber)的onNext方法便会执行:
static final class MapSubscriber<T, R> extends Subscriber<T> {
final Subscriber<? super R> actual;
final Func1<? super T, ? extends R> mapper;
boolean done;
public MapSubscriber(Subscriber<? super R> actual, Func1<? super T, ? extends R> mapper) {
this.actual = actual;
this.mapper = mapper;
}
@Override
public void onNext(T t) {
R result;
try {
// T 转化为 R
result = mapper.call(t);
} catch (Throwable ex) {
Exceptions.throwIfFatal(ex);
unsubscribe();
onError(OnErrorThrowable.addValueAsLastCause(ex, t));
return;
}
// 调用原来观察者(也就是例子中的observer)的onNext方法
actual.onNext(result);
}
...
}
onNext方法中,先调用mapper的call方法把String转为Integer,然后把Integer的结果传给原来观察者(也就是例子中的observer)的onNext方法中执行。
map变换的基本流程解析完毕~~