Android-RxJavaAndroid-Rxjava&retrofit&daggerRxJava

Rxjava2~take~timer~interval~buff

2017-06-26  本文已影响120人  品味与回味

take()


<pre>
Observable.just(1, 2, 3, 4, 5)
.subscribeOn(Schedulers.io())
// Be notified on the main thread
.observeOn(AndroidSchedulers.mainThread())
.take(3)
.subscribe(getObserver())

</pre>

输出没错是123

我们面来看看源码
直接来看ObservableTake的subscribeActual,[不懂的同学请看我前面的学渣系列]
<pre>
protected void subscribeActual(Observer<? super T> observer) {
source.subscribe(new TakeObserver<T>(observer, limit));
}
</pre>
这个source是ObservableSource的对象。 那么我们去找实现他的Observable
好吧 又回到了。
<pre>
public final void subscribe(Observer<? super T> observer)
subscribeActual(observer);
其他的省略了

</pre>
关键点一步,这回调用了谁的方法呢? 下面来揭晓
是ObservableObserveOn的subscribeActual
<pre>
@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<T>(observer, w, delayError, bufferSize));
    }
}

</pre>
看到了吗 又会调用
source.subscribe(new ObserveOnObserver<T>(observer, w, delayError, bufferSize));
然后 又要调用的是ObservableSubscribeOn的subscribeActual
<pre>
@Override
public void subscribeActual(final Observer<? super T> s) {
final SubscribeOnObserver<T> parent = new SubscribeOnObserver<T>(s);

    s.onSubscribe(parent);

    parent.setDisposable(scheduler.scheduleDirect(new Runnable() {
        @Override
        public void run() {
            source.subscribe(parent);
        }
    }));
}

</pre>

大家会好奇这两个地方为什么会被调用呢?
下面我给大家看一个地方
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
O(∩_∩)O
你没有看错
<pre>
public final Observable<T> observeOn(Scheduler scheduler) {
return observeOn(scheduler, false, bufferSize());
}

public final Observable<T> subscribeOn(Scheduler scheduler) {
ObjectHelper.requireNonNull(scheduler, "scheduler is null");
return RxJavaPlugins.onAssembly(new ObservableSubscribeOn<T>(this, scheduler));
}
</pre>
大家可以看到不。 这两个方法返回的也是Observable对象。 所以 他们会分别调用这两个对象subscribeActual方法。好吧,让我们来像下进行。
【下面是一个小扩展 给大家一个小小的感觉】
<pre>

Observable.just(1, 2, 3, 4, 5)

            .observeOn(AndroidSchedulers.mainThread())
            .take(3)
            .subscribe(getObserver())

看到有什么不同了吗? 我注释掉了一个方法。我为什么要这么干?我注视掉了那么
source.subscribe 会调用谁呢? 我直接给出来答案。大家可以思考一个 当我直接注释之后会调用just的subscribeActual

public final class ObservableFromArray<T> extends Observable<T> {
final T[] array;
public ObservableFromArray(T[] array) {
this.array = array;
}
@Override
public void subscribeActual(Observer<? super T> s) {
FromArrayDisposable<T> d = new FromArrayDisposable<T>(s, array);

    s.onSubscribe(d);

    if (d.fusionMode) {
        return;
    }

    d.run();
}

相信大家看过我之前的应该可以看懂。
</pre>

让我们回归正题当执行到ObservableSubscribeOn的subscribeActual的方法的时候

  public void subscribeActual(final Observer<? super T> s) {
    final SubscribeOnObserver<T> parent = new SubscribeOnObserver<T>(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
<pre>
public void subscribeActual(Observer<? super T> s) {
FromArrayDisposable<T> d = new FromArrayDisposable<T>(s, array);
s.onSubscribe(d);
if (d.fusionMode) {
return;
}
d.run();

</pre>
剩下的就好理解了,都是分别执行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的方法
<pre>
public Disposable schedulePeriodicallyDirect(@NonNull Runnable run, long initialDelay, long period, TimeUnit unit) {
PoolWorker w = pool.get().getEventLoop();
return w.schedulePeriodicallyDirect(run, initialDelay, period, unit);
}
</pre>

<pre>
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;
}
}

</pre>

分别设置了 什么时候开始。多长时间执行一次

4buffer

Observable<List<String>> buffered = getObservable().buffer(3, 2);

    buffered.subscribe(getObserver());

ObservableBuffer的subscribeActual的方法
<pre>

protected void subscribeActual(Observer<? super U> t) {
if (skip == count) {
BufferExactObserver<T, U> bes = new BufferExactObserver<T, U>(t, count, bufferSupplier);
if (bes.createBuffer()) {
source.subscribe(bes);
}
} else {
source.subscribe(new BufferSkipObserver<T, U>(t, count, skip, bufferSupplier));
}
}
</pre>
好吧到了关键的地方 source.subscribe是调用谁的地方
Observable.just("one", "two", "three", "four", "five");
所以是ObservableFromArray的subscribeActual方法

<pre>
public void subscribeActual(Observer<? super T> s) {
FromArrayDisposable<T> d = new FromArrayDisposable<T>(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();
        }
    }

</pre>

看到这个for方法了吗 这个就是决定你跳过的数量的。

5filter

Paste_Image.png

这个相信大家很熟悉,对就是过滤
<pre>
fromArray(1, 0, 6)
.filter(new Predicate<Integer>() {
@Override
public boolean test(Integer integer) throws Exception {
return integer.intValue() > 5;
}
})
</pre>

这里只是放出来关键代码
ObservableFilter的onNext
<pre>
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);
}
}

</pre>
这个b就是你的过滤条件。 下面的就是判断。 不符合的就不执行 actual.onNext(t);其实很简单的方式

6skip

和上面同理关键部分ObservableSkip的onNext方法
<pre>

public void onNext(T t) {
if (remaining != 0L) {
remaining--;
} else {
actual.onNext(t);
}
}
</pre>

7 scan

Paste_Image.png

RxJava的scan()函数可以看做是一个累加器函数。scan()函数对原始Observable发射的每一项数据都应用一个函数,它将函数的结果填充回可观测序列,等待和下一次发射的数据一起使用。

关键代码
<pre>
@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);
        }
    }

</pre>
执行的时候value 会累加。 a.onNext(u);在发射出去

8 replay

<pre>
PublishSubject<Integer> source = PublishSubject.create();
ConnectableObservable<Integer> connectableObservable = 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());

</pre>
replay 这个是缓存操作。
第二次订阅之后,就是缓存后面两个数据

9concat

Paste_Image.png

<pre>
final String[] aStrings = {"A1", "A2", "A3", "A4"};
final String[] bStrings = {"B1", "B2", "B3"};

    final Observable<String> aObservable = Observable.fromArray(aStrings);
    final Observable<String> bObservable = Observable.fromArray(bStrings);

    Observable.concat(aObservable, bObservable)
            .subscribe(getObserver());

</pre>

他的过程是
<pre>
return RxJavaPlugins.onAssembly(new ObservableConcatMap(fromArray(sources), Functions.identity(), bufferSize(), ErrorMode.BOUNDARY));
</pre>

concat操作符肯定也是有序的,实际上fromArray(sources)这么一个过程。

10merge

Paste_Image.png

<pre>
final String[] aStrings = {"A1", "A2", "A3", "A4"};
final String[] bStrings = {"B1", "B2", "B3"};

    final Observable<String> aObservable = Observable.fromArray(aStrings);
    final Observable<String> bObservable = Observable.fromArray(bStrings);

    Observable.merge(aObservable, bObservable)
            .subscribe(getObserver());

</pre>

无序的合并

11distinct 去除重复的

Paste_Image.png

<pre>
enum HashSetCallable implements Callable<Set<Object>> {
INSTANCE;
@Override
public Set<Object> call() throws Exception {
return new HashSet<Object>();
}
}
</pre>
HashSet中 是不允许重复元素的

12last

Paste_Image.png

<pre>
private void doSomeWork() {
getObservable().last("A1") // the default item ("A1") to emit if the source ObservableSource is empty
.subscribe(getObserver());
}

private Observable<String> getObservable() {
    return Observable.just("A1", "A2", "A3", "A4", "A5", "A6");
}

打印出来的是a6

</pre>

ObservableFromArray的run方法
<pre>
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();
        }
    }

</pre>

ObservableLastSingle的onComplete
<pre>
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());
}
}

</pre>

last方法会返回Single

13throttleFirst

Paste_Image.png

<pre>
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<Integer> getObservable() {
    return Observable.create(new ObservableOnSubscribe<Integer>() {
        @Override
        public void subscribe(ObservableEmitter<Integer> 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();
        }
    });
}

</pre>

从这个可以理解到发送第一个之后。剩下的500之后才会接受第二个

14throttleLast

Paste_Image.png

从这个可以看出来,这是在一段时间内接受最后一个数据
<pre>
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<Integer> getObservable() {
    return Observable.create(new ObservableOnSubscribe<Integer>() {
        @Override
        public void subscribe(ObservableEmitter<Integer> 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();
        }
    });
}

</pre>

15debounce

<pre>

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<Integer> getObservable() {
    return Observable.create(new ObservableOnSubscribe<Integer>() {
        @Override
        public void subscribe(ObservableEmitter<Integer> 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();
        }
    });

</pre>

Paste_Image.png

这个接受的一一个时间跨度之内的数据

16window

Paste_Image.png

可以看出来大概 的意思就是截取被观察者组成一个新的被观察者

17delay

Paste_Image.png
上一篇下一篇

猜你喜欢

热点阅读