昨天总结了RxJava2中Observable.create().subscribe()源码,没看的小伙伴可以看看昨天的博客地址,今天我们来学习下map,如果看懂了昨天的,那么今天的这个map很快就吸收了,我先放张图片,思想跟昨天的一样
Observable.create(
//===============这里是第一部分A===============
new ObservableOnSubscribe() {
@Override
public void subscribe(ObservableEmitter e) throws Exception {
e.onNext("你好世界");
e.onComplete();
}
}
//===============这里是第一部分A-结束===============
).map(
//===============这里是第二部分B===============
new Function() {
@Override
public Integer apply(String s) {
return Integer.parseInt(s);
}
//===============这里是第二部分B===============
}).subscribe(
//===============这里是第三部分c==============
new Observer() {
@Override
public void onSubscribe(Disposable d) {
}
@Override
public void onNext(Integer value) {
}
@Override
public void onError(Throwable e) {
}
@Override
public void onComplete() {
}
}
//===============这里是第三部分c==============
);
分为3大部分A B C,关于A部分的我就不讲解了,因为昨天已经涉及到了,今天讲解B部分
老规矩看看.map里面的源码
public final Observable map(Function super T, ? extends R> mapper) {
ObjectHelper.requireNonNull(mapper, "mapper is null");
//继续点击ObservableMap,这里的this表示的上游的Observable,也就是我们的A
//因为我们调用的是 Observable.map方法,所以mapper表示的就是B部分了
return RxJavaPlugins.onAssembly(new ObservableMap(this, mapper));
}
Function接口代码,这个没什么说的
public interface Function {
R apply(T t) throws Exception;
}
ObservableMap类代码:
public final class ObservableMap extends AbstractObservableWithUpstream {
final Function super T, ? extends U> function;
public ObservableMap(ObservableSource source, Function super T, ? extends U> function) {
//保存了上游Observable
super(source);
this.function = function;//我们的B部分
}
这里我们看到了AbstractObservableWithUpstream,它是一个抽象类,其实它也是Observable因为他继承了Observable
abstract class AbstractObservableWithUpstream extends Observable implements HasUpstreamObservableSource {
protected final ObservableSource source;
AbstractObservableWithUpstream(ObservableSource source) {
this.source = source;
}
@Override
public final ObservableSource source() {
return source;
}
}
所以我们可以把AbstractObservableWithUpstream当做是Observable。
刚刚我们的this,传递给了ObservableSource,ObservableSource它是一个接口来着
public interface ObservableSource {
void subscribe(Observer super T> observer);
}
刚好我们Observable这个抽象类实现了ObservableSource这个接口
public abstract class Observable implements ObservableSource{
//....省略很多代码,贴出实现方法
//等会我们讲解Observable.create().map.subscribe()的时候会讲到这个方法
public final void subscribe(Observer super T> observer) {
ObjectHelper.requireNonNull(observer, "observer is null");
try {
observer = RxJavaPlugins.onSubscribe(this, observer);
ObjectHelper.requireNonNull(observer, "Plugin returned null Observer");
//subscribeActual这个方法很重要,跟我们昨天讲的Observable.create().subscrible调用一样
subscribeActual(observer);
} catch (NullPointerException e) {
throw e;
} catch (Throwable e) {
Exceptions.throwIfFatal(e);
RxJavaPlugins.onError(e);
NullPointerException npe = new NullPointerException("Actually not, but can't throw other exceptions due to RS");
npe.initCause(e);
throw npe;
}
}
记得开头我的图片么,我们还有一个C部分没说,它是Observable.create().map.subscribe(new Observer
我们点击这个subscribe进入,不就上面我们说的那部分代码么?在subscribe方法里面它就会执行subscribeActual(observer);再次点击这个subscribeActual,发现是一个抽象类
protected abstract void subscribeActual(Observer super T> observer);
那么它的实现在哪呢?大家记得ObservableMap这个类么?
它继承了AbstractObservableWithUpstream,AbstractObservableWithUpstream继承Observable,所以实现类就在ObservableMap里面了,如下代码
//我们又回到了ObservableMap类里面了
public final class ObservableMap extends AbstractObservableWithUpstream {
final Function super T, ? extends U> function;
public ObservableMap(ObservableSource source, Function super T, ? extends U> function) {
super(source);
this.function = function;
}
@Override
public void subscribeActual(Observer super U> t) {
//接下来会去执行new MapObserver(t, function),
//然后再source.subscribe()(这里关键了),会去调用A部分的subscribeActual,并将MapObserver传递给CreateEmitter里面的observer
source.subscribe(new MapObserver(t, function));
}
//
static final class MapObserver extends BasicFuseableObserver {
final Function super T, ? extends U> mapper;
MapObserver(Observer super U> actual, Function super T, ? extends U> mapper) {
super(actual);
this.mapper = mapper;
}
@Override
public void onNext(T t) {
if (done) {
return;
}
if (sourceMode != NONE) {
actual.onNext(null);
return;
}
U v;
try {
v = ObjectHelper.requireNonNull(mapper.apply(t), "The mapper function returned a null value.");
} catch (Throwable ex) {
fail(ex);
return;
}
actual.onNext(v);
}
//省略..............
}
//这里有个BasicFuseableObserver,我们看看代码
abstract class BasicFuseableObserver implements Observer, QueueDisposable{
//省略..............
}
上面代码里面我们知道它进入了subscribeActual方法后,回去new MapObserver,并且传递2参数 t和function,t是什么不就是我们的C部分么
,function是什么,不就是我们的B部分么,MapObserver 它继承了BasicFuseableObserver,BasicFuseableObserver里面是实现了observer的接口,也就是实现了onComplete onError.......这些方法,没有实现onNext, onNext的实现,交给了MapObserver它是BasicFuseableObserver的继承者。看看BasicFuseableObserver里面的实现Observer接口
BasicFuseableObserver的类
@Override
public void onError(Throwable t) {
if (done) {
RxJavaPlugins.onError(t);
return;
}
done = true;
actual.onError(t);
}
@Override
public void onComplete() {
if (done) {
return;
}
done = true;
actual.onComplete();
}
回过头来继续看ObservableMap类里面的代码
ObservableMap类
public void subscribeActual(Observer super U> t) {
//接下来会去执行new MapObserver(t, function),
//然后再source.subscribe()(这里关键了)
source.subscribe(new MapObserver(t, function));
}
我们刚刚分析完了new MapObserver
ObservableCreate 源码
@Override
protected void subscribeActual(Observer super T> observer) {
CreateEmitter parent = new CreateEmitter(observer);
observer.onSubscribe(parent);
try {
source.subscribe(parent);
} catch (Throwable ex) {
Exceptions.throwIfFatal(ex);
parent.onError(ex);
}
}
static final class CreateEmitter
extends AtomicReference
implements ObservableEmitter, Disposable {
private static final long serialVersionUID = -3434801548987643227L;
final Observer super T> observer;
CreateEmitter(Observer super T> observer) {
this.observer = observer; //这里是将MapObserver传递进来了,
}
@Override
public void onNext(T t) {
if (t == null) {
onError(new NullPointerException("onNext called with null. Null values are generally not allowed in 2.x operators and sources."));
return;
}
if (!isDisposed()) {
observer.onNext(t);//这里调用的是ObservableMap里面的onNext,
//因为MapObserver实现了Observer的接口onNext
}
}
@Override
public void onError(Throwable t) {
if (t == null) {
t = new NullPointerException("onError called with null. Null values are generally not allowed in 2.x operators and sources.");
}
if (!isDisposed()) {
try {
observer.onError(t);
} finally {
dispose();
}
} else {
RxJavaPlugins.onError(t);
}
}
@Override
public void onComplete() {
if (!isDisposed()) {
try {
observer.onComplete();
} finally {
dispose();
}
}
}
我们在Activity里面这样调用的
Observable.create(
//===============这里是第一部分A===============
new ObservableOnSubscribe() {
@Override
public void subscribe(ObservableEmitter e) throws Exception {
//这里会调用CreateEmitter里面的onNext方法,
//因为它实现了ObservableEmitter
//最终会调用ObservableMap里面的onNext
e.onNext("1");
e.onComplete();
}
}
//===============这里是第一部分A-结束===============
).map(
.....省略
);
.....省略
MapObserver类
static final class MapObserver extends BasicFuseableObserver {
final Function super T, ? extends U> mapper;
MapObserver(Observer super U> actual, Function super T, ? extends U> mapper) {
super(actual);
this.mapper = mapper;
}
//直接调用这里
@Override
public void onNext(T t) {
if (done) {
return;
}
if (sourceMode != NONE) {
actual.onNext(null);
return;
}
U v;
try {
v = ObjectHelper.requireNonNull(mapper.apply(t), "The mapper function returned a null value.");
} catch (Throwable ex) {
fail(ex);
return;
}
actual.onNext(v); //这里的actual就是C部分的代码
}
actual始终表示我们的下一级
总的来说流程是这样的
Observable.create(A).map(B).subscribe(C)
从create(A)---->进入到Observable,点击new ObservableCreate
从map(B)----->进入到Observable,点击new ObservableMap
从subscribe(C)----->进入到Observable,发现里面有subscribeActual,它是一个抽象类,它的实现在ObservableMap完成,所以此时我们的注意力应该在ObservableMap的subscribeActual的MapObserver类里面,t 在上面已经描述了它是C部分,function是B,那么这样传递下来后,在MapObserver的构造函数里,t传给了actual为=C,function传给了mapper=B。我们要知道MapObserver类的继承类BasicFuseableObserver实现了Observer,所以它实现了onError,onComplete,偏偏没有实现onNext,它的实现就在MapObserver类里面。
@Override
public void subscribeActual(Observer super U> t) {
source.subscribe(new MapObserver(t, function));
}
source是我们的A部分,其实就是调用A类的Observable
@SchedulerSupport(SchedulerSupport.NONE)
@Override
public final void subscribe(Observer super T> observer) {
ObjectHelper.requireNonNull(observer, "observer is null");
try {
observer = RxJavaPlugins.onSubscribe(this, observer);
ObjectHelper.requireNonNull(observer, "Plugin returned null Observer");
subscribeActual(observer);//看到这里没有,调用这个抽象类
} catch (NullPointerException e) { // NOPMD
throw e;
} catch (Throwable e) {
Exceptions.throwIfFatal(e);
RxJavaPlugins.onError(e);
NullPointerException npe = new NullPointerException("Actually not, but can't throw other exceptions due to RS");
npe.initCause(e);
throw npe;
}
}
subscribeActual(observer);注意这里的这句代码,然后就会调用ObservableCreate里面的subscribeActual
@Override
protected void subscribeActual(Observer super T> observer) {
//CreateEmitter实现了ObservableEmitter,
//当我们在A中调用onNext其实就是调用我妈妈的CreateEmitter实现的方法
//这里面的observer是我们的MapObserver
CreateEmitter parent = new CreateEmitter(observer);
observer.onSubscribe(parent);
try {
source.subscribe(parent);
} catch (Throwable ex) {
Exceptions.throwIfFatal(ex);
parent.onError(ex);
}
}
static final class CreateEmitter
extends AtomicReference
implements ObservableEmitter, Disposable {
private static final long serialVersionUID = -3434801548987643227L;
final Observer super T> observer;
CreateEmitter(Observer super T> observer) {
this.observer = observer;//我们的MapObserver
}
@Override
public void onNext(T t) {
if (t == null) {
onError(new NullPointerException("onNext called with null. Null values are generally not allowed in 2.x operators and sources."));
return;
}
if (!isDisposed()) {
observer.onNext(t);//调用MapObserver的接口
}
}
@Override
public void onError(Throwable t) {
if (t == null) {
t = new NullPointerException("onError called with null. Null values are generally not allowed in 2.x operators and sources.");
}
if (!isDisposed()) {
try {
observer.onError(t);
} finally {
dispose();
}
} else {
RxJavaPlugins.onError(t);
}
}
@Override
public void onComplete() {
if (!isDisposed()) {
try {
observer.onComplete();
} finally {
dispose();
}
}
}
好了今天的map讲完了,谢谢大家。
下一篇文章subscribeOn:http://www.jianshu.com/p/4adb8df73a9c