take()
Observable.just(1, 2, 3, 4, 5)
.subscribeOn(Schedulers.io())
// Be notified on the main thread
.observeOn(AndroidSchedulers.mainThread())
.take(3)
.subscribe(getObserver())
输出没错是123
我们面来看看源码
直接来看ObservableTake的subscribeActual,[不懂的同学请看我前面的学渣系列]
protected void subscribeActual(Observer super T> observer) {
source.subscribe(new TakeObserver(observer, limit));
}
这个source是ObservableSource的对象。 那么我们去找实现他的Observable
好吧 又回到了。
public final void subscribe(Observer super T> observer)
subscribeActual(observer);
其他的省略了
关键点一步,这回调用了谁的方法呢? 下面来揭晓
是ObservableObserveOn的subscribeActual
@Override
protected void subscribeActual(Observer super T> observer) {
if (scheduler instanceof TrampolineScheduler) {
source.subscribe(observer);
} else {
Scheduler.Worker w = scheduler.createWorker();source.subscribe(new ObserveOnObserver
(observer, w, delayError, bufferSize)); } }
看到了吗 又会调用
source.subscribe(new ObserveOnObserver
然后 又要调用的是ObservableSubscribeOn的subscribeActual
@Override
public void subscribeActual(final Observer super T> s) {
final SubscribeOnObserverparent = new SubscribeOnObserver (s); s.onSubscribe(parent); parent.setDisposable(scheduler.scheduleDirect(new Runnable() { @Override public void run() { source.subscribe(parent); } })); }
大家会好奇这两个地方为什么会被调用呢?
下面我给大家看一个地方
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
O(∩_∩)O
你没有看错
public final ObservableobserveOn(Scheduler scheduler) {
return observeOn(scheduler, false, bufferSize());
}public final Observable
subscribeOn(Scheduler scheduler) {
ObjectHelper.requireNonNull(scheduler, "scheduler is null");
return RxJavaPlugins.onAssembly(new ObservableSubscribeOn(this, scheduler));
}
大家可以看到不。 这两个方法返回的也是Observable对象。 所以 他们会分别调用这两个对象subscribeActual方法。好吧,让我们来像下进行。
【下面是一个小扩展 给大家一个小小的感觉】
Observable.just(1, 2, 3, 4, 5)
.observeOn(AndroidSchedulers.mainThread()) .take(3) .subscribe(getObserver())
看到有什么不同了吗? 我注释掉了一个方法。我为什么要这么干?我注视掉了那么
source.subscribe 会调用谁呢? 我直接给出来答案。大家可以思考一个 当我直接注释之后会调用just的subscribeActualpublic final class ObservableFromArray
extends Observable {
final T[] array;
public ObservableFromArray(T[] array) {
this.array = array;
}
@Override
public void subscribeActual(Observer super T> s) {
FromArrayDisposabled = new FromArrayDisposable (s, array); s.onSubscribe(d); if (d.fusionMode) { return; } d.run(); }
相信大家看过我之前的应该可以看懂。
让我们回归正题当执行到ObservableSubscribeOn的subscribeActual的方法的时候
public void subscribeActual(final Observer super T> s) {
final SubscribeOnObserver parent = new SubscribeOnObserver(s);
s.onSubscribe(parent);
parent.setDisposable(scheduler.scheduleDirect(new Runnable() {
@Override
public void run() {
source.subscribe(parent);
}
}));
source.subscribe(parent); 看到这个方法了吗、首先它是异步的。另外执行
.source.subscribe(parent);的时候 ,实际上就执行了ObservableFromArray的subscribeActual
public void subscribeActual(Observer super T> s) {
FromArrayDisposabled = new FromArrayDisposable (s, array);
s.onSubscribe(d);
if (d.fusionMode) {
return;
}
d.run();
剩下的就好理解了,都是分别执行onnext等方法。
到这里task的大体思路介绍完毕
2下面开始timer 定时器
public Disposable scheduleDirect(@NonNull Runnable run, long delay, @NonNull TimeUnit unit) {
final Worker w = createWorker();
final Runnable decoratedRun = RxJavaPlugins.onSchedule(run);
w.schedule(new Runnable() {
@Override
public void run() {
try {
decoratedRun.run();
} finally {
w.dispose();
}
}
}, delay, unit);
return w;
}
重复的就不贴了。 都是差不多重复的。 只是给大家贴上关键代码
看到这里面了吗。delay 就是大家贴上的时间。 详细这个大家都是可以看明白的。,
3interval
做周期性操作,从翻译上大家就应该可以看明白
ComputationScheduler的schedulePeriodicallyDirect的方法
public Disposable schedulePeriodicallyDirect(@NonNull Runnable run, long initialDelay, long period, TimeUnit unit) {
PoolWorker w = pool.get().getEventLoop();
return w.schedulePeriodicallyDirect(run, initialDelay, period, unit);
}
NewThreadWorker的schedulePeriodicallyDirect的方法
public Disposable schedulePeriodicallyDirect(final Runnable run, long initialDelay, long period, TimeUnit unit) {
Runnable decoratedRun = RxJavaPlugins.onSchedule(run);
try {
Future> f = executor.scheduleAtFixedRate(decoratedRun, initialDelay, period, unit);
return Disposables.fromFuture(f);
} catch (RejectedExecutionException ex) {
RxJavaPlugins.onError(ex);
return EmptyDisposable.INSTANCE;
}
}
分别设置了 什么时候开始。多长时间执行一次
4buffer
Observable> buffered = getObservable().buffer(3, 2);
buffered.subscribe(getObserver());
ObservableBuffer的subscribeActual的方法
protected void subscribeActual(Observer super U> t) {
if (skip == count) {
BufferExactObserverbes = new BufferExactObserver (t, count, bufferSupplier);
if (bes.createBuffer()) {
source.subscribe(bes);
}
} else {
source.subscribe(new BufferSkipObserver(t, count, skip, bufferSupplier));
}
}
好吧到了关键的地方 source.subscribe是调用谁的地方
Observable.just("one", "two", "three", "four", "five");
所以是ObservableFromArray的subscribeActual方法
public void subscribeActual(Observer super T> s) {
FromArrayDisposabled = new FromArrayDisposable (s, array); s.onSubscribe(d); if (d.fusionMode) { return; } d.run(); }
void run() {
T[] a = array;
int n = a.length;for (int i = 0; i < n && !isDisposed(); i++) { T value = a[i]; if (value == null) { actual.onError(new NullPointerException("The " + i + "th element is null")); return; } actual.onNext(value); } if (!isDisposed()) { actual.onComplete(); } }
看到这个for方法了吗 这个就是决定你跳过的数量的。
5filter
这个相信大家很熟悉,对就是过滤
fromArray(1, 0, 6)
.filter(new Predicate() {
@Override
public boolean test(Integer integer) throws Exception {
return integer.intValue() > 5;
}
})
这里只是放出来关键代码
ObservableFilter的onNext
public void onNext(T t) {
if (sourceMode == NONE) {
boolean b;
try {
b = filter.test(t);
} catch (Throwable e) {
fail(e);
return;
}
if (b) {
actual.onNext(t);
}
} else {
actual.onNext(null);
}
}
这个b就是你的过滤条件。 下面的就是判断。 不符合的就不执行 actual.onNext(t);其实很简单的方式
6skip
和上面同理关键部分ObservableSkip的onNext方法
public void onNext(T t) {
if (remaining != 0L) {
remaining--;
} else {
actual.onNext(t);
}
}
7 scan
RxJava的scan()函数可以看做是一个累加器函数。scan()函数对原始Observable发射的每一项数据都应用一个函数,它将函数的结果填充回可观测序列,等待和下一次发射的数据一起使用。
关键代码
@Override
public void onNext(T t) {
if (done) {
return;
}
final Observer super T> a = actual;
T v = value;
if (v == null) {
value = t;
a.onNext(t);
} else {
T u;try { u = ObjectHelper.requireNonNull(accumulator.apply(v, t), "The value returned by the accumulator is null"); } catch (Throwable e) { Exceptions.throwIfFatal(e); s.dispose(); onError(e); return; } value = u; a.onNext(u); } }
执行的时候value 会累加。 a.onNext(u);在发射出去
8 replay
PublishSubjectsource = PublishSubject.create();
ConnectableObservableconnectableObservable = source.replay(2); // bufferSize = 3 to retain 3 values to replay
connectableObservable.connect(); // connecting the connectableObservable
connectableObservable.subscribe(getFirstObserver());
source.onNext(1);
source.onNext(2);
source.onNext(3);
source.onNext(4);
source.onComplete();/* * it will emit 2, 3, 4 as (count = 3), retains the 3 values for replay */ connectableObservable.subscribe(getSecondObserver());
replay 这个是缓存操作。
第二次订阅之后,就是缓存后面两个数据
9concat
final String[] aStrings = {"A1", "A2", "A3", "A4"};
final String[] bStrings = {"B1", "B2", "B3"};final Observable
aObservable = Observable.fromArray(aStrings); final Observable bObservable = Observable.fromArray(bStrings); Observable.concat(aObservable, bObservable) .subscribe(getObserver());
他的过程是
return RxJavaPlugins.onAssembly(new ObservableConcatMap(fromArray(sources), Functions.identity(), bufferSize(), ErrorMode.BOUNDARY));
concat操作符肯定也是有序的,实际上fromArray(sources)这么一个过程。
10merge
final String[] aStrings = {"A1", "A2", "A3", "A4"};
final String[] bStrings = {"B1", "B2", "B3"};final Observable
aObservable = Observable.fromArray(aStrings); final Observable bObservable = Observable.fromArray(bStrings); Observable.merge(aObservable, bObservable) .subscribe(getObserver());
无序的合并
11distinct 去除重复的
enum HashSetCallable implements Callable> {
INSTANCE;
@Override
public Set
HashSet中 是不允许重复元素的
12last
private void doSomeWork() {
getObservable().last("A1") // the default item ("A1") to emit if the source ObservableSource is empty
.subscribe(getObserver());
}private Observable
getObservable() { return Observable.just("A1", "A2", "A3", "A4", "A5", "A6"); } 打印出来的是a6
ObservableFromArray的run方法
void run() {
T[] a = array;
int n = a.length;for (int i = 0; i < n && !isDisposed(); i++) { T value = a[i]; if (value == null) { actual.onError(new NullPointerException("The " + i + "th element is null")); return; } actual.onNext(value); } if (!isDisposed()) { actual.onComplete(); } }
ObservableLastSingle的onComplete
public void onComplete() {
s = DisposableHelper.DISPOSED;
T v = item;
if (v != null) {
item = null;
actual.onSuccess(v);
} else {
v = defaultItem;
if (v != null) {
actual.onSuccess(v);
} else {
actual.onError(new NoSuchElementException());
}
}
last方法会返回Single
13throttleFirst
private void doSomeWork() {
getObservable()
.throttleFirst(500, TimeUnit.MILLISECONDS)
// Run on a background thread
.subscribeOn(Schedulers.io())
// Be notified on the main thread
.observeOn(AndroidSchedulers.mainThread())
.subscribe(getObserver());
}private Observable
getObservable() { return Observable.create(new ObservableOnSubscribe () { @Override public void subscribe(ObservableEmitter emitter) throws Exception { // send events with simulated time wait Thread.sleep(0); emitter.onNext(1); // skip emitter.onNext(2); // deliver Thread.sleep(505); emitter.onNext(3); // skip Thread.sleep(99); emitter.onNext(4); // skip Thread.sleep(100); emitter.onNext(5); // skip emitter.onNext(6); // deliver Thread.sleep(305); emitter.onNext(7); // deliver Thread.sleep(510); emitter.onComplete(); } }); }
从这个可以理解到发送第一个之后。剩下的500之后才会接受第二个
14throttleLast
从这个可以看出来,这是在一段时间内接受最后一个数据
getObservable()
.throttleLast(500, TimeUnit.MILLISECONDS)
// Run on a background thread
.subscribeOn(Schedulers.io())
// Be notified on the main thread
.observeOn(AndroidSchedulers.mainThread())
.subscribe(getObserver());
}private Observable
getObservable() { return Observable.create(new ObservableOnSubscribe () { @Override public void subscribe(ObservableEmitter emitter) throws Exception { // send events with simulated time wait Thread.sleep(0); emitter.onNext(1); // skip emitter.onNext(2); // deliver Thread.sleep(505); emitter.onNext(3); // skip Thread.sleep(99); emitter.onNext(4); // skip Thread.sleep(100); emitter.onNext(5); // skip emitter.onNext(6); // deliver Thread.sleep(305); emitter.onNext(7); // deliver Thread.sleep(510); emitter.onComplete(); } }); }
15debounce
getObservable() .debounce(500, TimeUnit.MILLISECONDS) // Run on a background thread .subscribeOn(Schedulers.io()) // Be notified on the main thread .observeOn(AndroidSchedulers.mainThread()) .subscribe(getObserver()); } private Observable
getObservable() { return Observable.create(new ObservableOnSubscribe () { @Override public void subscribe(ObservableEmitter emitter) throws Exception { // send events with simulated time wait emitter.onNext(1); // skip Thread.sleep(400); emitter.onNext(2); // deliver Thread.sleep(505); emitter.onNext(3); // skip Thread.sleep(100); emitter.onNext(4); // deliver Thread.sleep(605); emitter.onNext(5); // deliver Thread.sleep(510); emitter.onComplete(); } });
这个接受的一一个时间跨度之内的数据
16window
可以看出来大概 的意思就是截取被观察者组成一个新的被观察者