这个当成只是当成一个临时笔记,可能会些错误
一、先看一个常见的流程。
Observable<String> observable1 = Observable.just("aa");
observable1.subscribe(new Subscriber<String>() {
@Override
public void onCompleted() {
}
@Override
public void onError(Throwable e) {
}
@Override
public void onNext(String s) {
System.out.println(s);
}
});
拆分
1、创建:Observable observable = Observable.just(“aa”);
看源码可知是创建了一个Observable的子类ScalarSynchronousObservable
protected ScalarSynchronousObservable(final T t) {
super(hook.onCreate(new JustOnSubscribe<T>(t)));
this.t = t;
}
这里为了让分析更简单点,我们就把ScalarSynchronousObservable忽略掉,直接当成Observable来看代(大致说下ScalarSynchronousObservable的作用用来处理只有一个Subscriber的Observable)。
这里是调用了父类Observable的构造方法
protected Observable(OnSubscribe<T> f) {
this.onSubscribe = f;
}
OnSubscribe类只有一个它父类Action1的抽象方法
public interface Action1<T> extends Action {
void call(T t);
}
继续看源码发现给Observable的构造函数传入的是一个JustOnSubscribe对象
static final class JustOnSubscribe<T> implements OnSubscribe<T> {
final T value;
//value是要处理的对象,即我们上文传的字符串"aa"
JustOnSubscribe(T value) {
this.value = value;
}
@Override
public void call(Subscriber<? super T> s) {
s.setProducer(createProducer(s, value));
}
}
重点看下call方法。这里是给事件的通知者设置一个制造者,
createProducer(s, value)最终是创造了一个WeakSingleProducer对象
public WeakSingleProducer(Subscriber<? super T> actual, T value) {
//事件的通知者
this.actual = actual;
//要处理的对象
this.value = value;
}
到这里,整个创建过程也到了尾声。
我们现在看看对象之间的关系是啥样
Observable{
onSubscribe = JustOnSubscribe{
value = "aa"
@Override
public void call(Subscriber<? super T> s) {
s.setProducer(createProducer(s, value));
}
}
}
2、subscribe过程、贴核心代码
static <T> Subscription subscribe(Subscriber<? super T> subscriber, Observable<T> observable) {
//调用事件通知者的onStart()方法
subscriber.onStart();
//subscriber不属于SafeSubscriber就将subscriber重新指向一个新的SafeSubscriber
if (!(subscriber instanceof SafeSubscriber)) {
subscriber = new SafeSubscriber<T>(subscriber);
}
try {
//这里与observable.onSubscribe).call(subscriber)等价
//在上文提到的对象之间的关系中可以看出是调用了JustOnSubscribe.call(Subscriber<? super T> s)方法。
//上面分析过call方法的作用了,这里也就是给subscriber对象中设置了WeakSingleProducer对象。然后调用了本身的setProducer方法。
hook.onSubscribeStart(observable, observable.onSubscribe).call(subscriber);
return hook.onSubscribeReturn(subscriber);
} catch (Throwable e) {
subscriber.onError(hook.onSubscribeError(e));
}
return Subscriptions.unsubscribed();
}
}
看下Subscriber.setProducer(Producer p) 方法
//如果上层还有订阅者,则走上层订阅者的setProducer,一直递归到最上层,再一层的一层的调用producer.request方法
public void setProducer(Producer p) {
long toRequest;
boolean passToSubscriber = false;
synchronized (this) {
toRequest = requested;
producer = p;
if (subscriber != null) {
if (toRequest == NOT_SET) {
passToSubscriber = true;
}
}
}
if (passToSubscriber) {
subscriber.setProducer(producer);
} else {
if (toRequest == NOT_SET) {
producer.request(Long.MAX_VALUE);
} else {
producer.request(toRequest);
}
}
}
从上面我们知道了这个producer,就是WeakSingleProducer类。直接看下他的request(long n)方法。
@Override
public void request(long n) {
if (once) {
return;
}
if (n < 0L) {
throw new IllegalStateException("n >= required but it was " + n);
}
if (n == 0L) {
return;
}
once = true;
Subscriber<? super T> a = actual;
if (a.isUnsubscribed()) {
return;
}
T v = value;
try {
a.onNext(v);
} catch (Throwable e) {
Exceptions.throwOrReport(e, a, v);
return;
}
if (a.isUnsubscribed()) {
return;
}
a.onCompleted();
}
可以看出经过一系统的判断,最后执行了OnNext()、onCompleted()方法。
看到这,我们再回想一下整个执行过程。
observable1 = Observable{
onSubscribe= JustOnSubscribe{
"aa"
}
}
subscriber1 = Subscriber{
subscriber = null
subscriptions = SubscriptionList;
}
-->
observable1.subscribe(safeSubscriber1{
subscriber = subscriber1
})
observable1.onSubscribe.call(safeSubscriber1)
-->
subscriber1.setProducer(weakSingleProducer1(safeSubscriber1,"aa"))
-->
safeSubscriber1.subscriber1.setProducer(weakSingleProducer1)
safeSubscriber1.subscriber1.producer.request()
safeSubscriber1.subscriber1.producer.actual.onNext
safeSubscriber1.subscriber1.producer.actual.onCompleted
-->
over
添加一个filter,再观察下流程是什么样的
Observable<String> observable1 = Observable.just("aa");
Observable<String> observable2 = observable1.filter(new Func1<String, Boolean>() {
@Override
public Boolean call(String s) {
return true;
}
});
Subscriber<String> subscriber = new Subscriber<String>() {
//xxx
};
observable2.subscribe(subscriber);
observable1 = Observable{
onSubscribe= JustOnSubscribe{
"aa"
}
}
像上面一样跟踪一下过程
observable1 = Observable{
onSubscribe= JustOnSubscribe{
"aa"
}
}
-->
observable2 = Observable{
onSubscribe=OnSubscribeLift{
parent=observable1.onSubscribe
operator = OperatorFilter{
predicate = 上面的匿名内部类Func1
}
}
}
-->
subscriber = Subscriber{
subscriber = null
subscriptions = SubscriptionList;
}
observable2.subscribe(subscriber);
subscriber.onStart()
subscriber = SafeSubscriber{
subscriber = subscriber
}
observable2.OnSubscribeLift.call(subscriber)
-->
observable2.OnSubscribeLift.operator.call(subscriber)
==
observable2.OnSubscribeLift.OperatorFilter.call(SafeSubscriber)
-->
SafeSubscriber.subscriptions.add{FilterSubscriber(SafeSubscriber,上面的匿名内部类Func1)}
FilterSubscriber.start()
observable1.onSubscribe.call(FilterSubscriber)
==
JustOnSubscribe.call(FilterSubscriber)
FilterSubscriber.setProducer(createProducer(s, value));
FilterSubscriber.actual(SafeSubscriber).setProducer