一,例子
Student[] students = ...; Subscriber<String> subscriber = new Subscriber<String>() { @Override public void onNext(String name) { Log.d(tag, name); } ... }; Observable.from(students) .map(new Func1<Student, String>() { @Override public String call(Student student) { return student.getName(); } }) .subscribe(subscriber);
二,从subscribe开始
private static <T> Subscription subscribe(Subscriber<? super T> subscriber, Observable<T> observable) {
// new Subscriber so onStart it subscriber.onStart(); 。。。。 try { // allow the hook to intercept and/or decorate <span style="color:#ff0000;"> hook.onSubscribeStart(observable, observable.onSubscribe).call(subscriber); //重要 </span> return hook.onSubscribeReturn(subscriber); } catch (Throwable e) { 。。。
三,分析上段红色标记的代码, 获取OnSubscriber对象,并调用其call方法,注意传入的参数为旧的subscriber对象;
四,这个OnSubscriber是原来的还是新创建的呢? 看map代码
public final <R> Observable<R> map(Func1<? super T, ? extends R> func) { return lift(new OperatorMap<T, R>(func)); }
五,调用的是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 { <span style="color:#ff0000;"> Subscriber<? super T> st = hook.onLift(operator).call(o); </span> try { // new Subscriber created and being subscribed with so 'onStart' it st.onStart(); onSubscribe.call(st); } catch (Throwable e) { 。。。。。。
原来map会创建一个新的Observable和onSubscribe对象,而在步骤三中所获取的正是这个新创建的OnSubscribe对象,也会调用这个对象的call方法;
分析call中的代码,其中有hook.onLift(operator).call(o); 这个operator正是步骤四中的:
new OperatorMap<T, R>(func)
而func是步骤一中的:
new Func1<Student, String>() { @Override public String call(Student student) { return student.getName(); } }
六,OperatorMap
public final class OperatorMap<T, R> implements Operator<R, T> { private final Func1<? super T, ? extends R> transformer; public OperatorMap(Func1<? super T, ? extends R> transformer) { this.transformer = transformer; } @Override public Subscriber<? super T> call(final Subscriber<? super R> o) { return new Subscriber<T>(o) { //重点 @Override public void onCompleted() { o.onCompleted(); } @Override public void onError(Throwable e) { o.onError(e); } @Override public void onNext(T t) { try { o.onNext(transformer.call(t)); //重点 } catch (Throwable e) { Exceptions.throwOrReport(e, this, t); } } }; } 1,创建 OperatorMap 对象时,传入 transformer,用于OnNext 参数的转化 2,operatorMap的作用就是根据旧的Subscriber对象,创建新的Subscriber对象
public interface Func1<T, R> extends Function { R call(T t); }
Operator /** * Operator function for lifting into an Observable. */ public interface Operator<R, T> extends Func1<Subscriber<? super R>, Subscriber<? super T>> { // cover for generics insanity }
七,总而言之,hook.onLift(operator).call(o)会创建一个新的Subscriber对象
八,回到第五步 onSubscribe.call(st);
这里会调用旧的OnSubscribe对象,并调用其call方法; 在这个方法中的st参数为上一步新创建的对象;
这个call方法中,调用new st.OnNext方法;
九,最后看 OperatorMap 对象中新创建的Subscribe
@Override public void onNext(T t) { try { o.onNext(transformer.call(t)); } catch (Throwable e) { Exceptions.throwOrReport(e, this, t); } }
而如果没有经过map转换,就会直接调用这里;