接下来我们分析Rxjava中的转换集合中的数据:
Observable.just(1 , 2 , 3)
.map(new Func1<Integer, String>() {
@Override
public String call(Integer integer) {
return integer + "qb";
}
})
.subscribe(new Observer<String>() {
@Override
public void onCompleted() {
}
@Override
public void onError(Throwable e) {
}
@Override
public void onNext(String s) {
LogUtils.e(TAG , "qb : " + s);
}
});
首先我们来看just方法,首先走了:
public static <T> Observable<T> just(T t1, T t2, T t3) {
return from((T[])new Object[] { t1, t2, t3 });
}
public static <T> Observable<T> from(T[] array) {
.........//这里不分析数据没有或者一个的情况了,主要走了这个
return create(new OnSubscribeFromArray<T>(array));//这里主要创建了一个新的Observable对象,并new了一个OnSubscribeFromArray对象
}
接下来我们看map方法
public final <R> Observable<R> map(Func1<? super T, ? extends R> func) {
return lift(new OperatorMap<T, R>(func));//先new 了一个OperatorMap对象,然后调用lift方法
}
public final <R> Observable<R> lift(final Operator<? extends R, ? super T> operator) {
//lift方法返回了一个Observable 对象,它又new了一个OnSubscribe对象
return new Observable<R>(new OnSubscribe<R>() {
@Override
public void call(Subscriber<? super R> o) {
//o对象是我们调用subscribe方法生成了SafeSubscriber对象
try {
//hook.onLift(operator)返回的还是operator这个对象,即我们之前传过来的OperatorMap对象,调用了改对象的call()方法。
Subscriber<? super T> st = hook.onLift(operator).call(o);
try {
// new Subscriber created and being subscribed with so 'onStart' it
st.onStart();
//onSubscribe这个是我们第一个OnSubscribeFromArray对象
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);
}
}
});
}
我们再来看我们最后调用的subscribe方法,
public final Subscription subscribe(final Observer<? super T> observer) {
................
//主要是调用了内部的一个subcribe方法并new了一个Subscriber对象
return subscribe(new Subscriber<T>() {
@Override
public void onCompleted() {
//我们传过来的oberserver对象回调
observer.onCompleted();
}
@Override
public void onError(Throwable e) {
observer.onError(e);
}
@Override
public void onNext(T t) {
observer.onNext(t);
}
});
}
和我们第一次分析的流程一样。主要是在
hook.onSubscribeStart(observable, observable.onSubscribe).call(subscriber);调用了我们使用map后生产的Observable对象的call方法。
try {
//hook.onLift(operator)返回的是OperatorMap对象,调用了call方法,并把SafeSubscriber对象保存到新的Subscriber对象中。
Subscriber<? super T> st = hook.onLift(operator).call(o);
try {
// new Subscriber created and being subscribed with so 'onStart' it
st.onStart();
//onSubscribe对象为OnSubscribeFromArray对象
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);
}
我们先分析 Subscriber<? super T> st = hook.onLift(operator).call(o);我们看call(o)方法干了些什么
public Subscriber<? super T> call(final Subscriber<? super R> o) {
//主要是new了一个Subscriber对象,并把SafeSubscriber对象保存下来,在合适的地方调用方法
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);
}
}
};
}
然后我们来看onSubscribe.call(st);这个方法,onSubcribe这个是OnSubcribeFromArray对象,并把我们在OperatorMap 调用call()方法返回的Subscriber对象传过去。我们来看下OnSubscribeFromArray对象的call方法干了什么:
public void call(Subscriber<? super T> child) {
//可以看到我们主要是new 了一个FromArrayProducer对象,并把OperatorMap生成的Subscriber对象和我们的数据传过去,然后调用了
OperatorMap生成的Subscriber的setProducer()方法
child.setProducer(new FromArrayProducer<T>(child, array));
}
我们接下来看setProducer方法干了什么:
public void setProducer(Producer p) {
long toRequest;
boolean passToSubscriber = false;//这个变量主要用来判断需不需要继续执行setProducer方法
synchronized (this) {
toRequest = requested;
producer = p;
if (subscriber != null) {//检测Subscriber对象中的上一个subscriber对象是否存在,主要是用来判断是否走到了最外层方法
// middle operator ... we pass thru unless a request has been made
if (toRequest == NOT_SET) {
// we pass-thru to the next producer as nothing has been requested
passToSubscriber = true;
}
}
}
// do after releasing lock
if (passToSubscriber) {
//调用Subscriber方法中传递过来的subscriber对象,吧producer一直传递下去,为了调用producer方法
subscriber.setProducer(producer);
} else {
//如果passToSubcriber为false,表示走到了最外层的subscriber构造方法传过来的对象weik
// we execute the request with whatever has been requested (or Long.MAX_VALUE)
if (toRequest == NOT_SET) {
//调用了我们之前new出来的FromArrayProducer方法中的request
producer.request(Long.MAX_VALUE);
} else {
producer.request(toRequest);
}
}
}
我们接下来看FromArrayProducere方法中的request走了什么:
public void request(long n) {
........
//我们可以看到主要是走了fastPath()方法
if (n == Long.MAX_VALUE) {
if (BackpressureUtils.getAndAddRequest(this, n) == 0) {
fastPath();
}
} else
if (n != 0) {
if (BackpressureUtils.getAndAddRequest(this, n) == 0) {
slowPath(n);
}
}
}
我们来分析slowPath()方法:
void slowPath(long r) {
//child对象为我们传过来的OperatorMap通过调用call(o)方法生成的Subscriber对象
final Subscriber<? super T> child = this.child;
final T[] array = this.array;
final int n = array.length;
long e = 0L;
int i = index;
for (;;) {
while (r != 0L && i != n) {
.......
//调用了onNext方法,也就是调用了OperatorMap调用call()方法生成的Subscriber对象的call(o)方法,也就是调用了call中传过来参数o对象的call方法,然后调用了SafeSubscriber中的next方法。最终调用了我们自己new的Observer方法中的onNext()方法。
child.onNext(array[i]);
i++;
if (i == n) {
if (!child.isUnsubscribed()) {
child.onCompleted();
}
return;
}
r--;
e--;
}
r = get() + e;
if (r == 0L) {
index = i;
r = addAndGet(e);
if (r == 0L) {
return;
}
e = 0L;
}
}
}
onError(),onCompleted()方法和onNext()方法差不多,读者可以按这个思路推理下去。下一篇就是RxJava的线程切换了。