1. 程式人生 > >RxJava2 Flowable delay (輔助操作符)

RxJava2 Flowable delay (輔助操作符)

delay (輔助操作符)

1 delay介面

<U> Flowable<T>

delay(Function<? super T,? extends Publisher<U>> itemDelayIndicator)

Returns a Flowable that delays the emissions of the source Publisher via another Publisher on a per-item basis.

返回一個Flowable,它基於每個專案通過另一個Publisher延遲源Publisher的排放。

Flowable<T>

delay(long delay, TimeUnit unit)

Returns a Flowable that emits the items emitted by the source Publisher shifted forward in time by a specified delay.

返回一個Flowable,它發出由源Publisher按時間向前移動指定延遲的項。

Flowable<T>

delay(long delay, TimeUnit unit, boolean delayError)

Returns a Flowable that emits the items emitted by the source Publisher shifted forward in time by a specified delay.

返回一個Flowable,它發出由源Publisher按時間向前移動指定延遲的項(並設定是否延遲error處理)

Flowable<T>

delay(long delay, TimeUnit unit, Scheduler scheduler)

Returns a Flowable that emits the items emitted by the source Publisher shifted forward in time by a specified delay.

返回一個Flowable,它發出由源Publisher按時間向前移動指定延遲的項。(延遲策略可以自己自定義Scheduler

Flowable<T>

delay(long delay, TimeUnit unit, Scheduler scheduler, boolean delayError)

Returns a Flowable that emits the items emitted by the source Publisher shifted forward in time by a specified delay.

返回一個Flowable,它發出由源Publisher按時間向前移動指定延遲的項。(延遲策略可以自己自定義Scheduler,並設定是否延遲error處理)

<U,V> Flowable<T>

delay(Publisher<U> subscriptionIndicator, Function<? super T,? extendsPublisher<V>> itemDelayIndicator)

Returns a Flowable that delays the subscription to and emissions from the source Publisher via another Publisher on a per-item basis.

返回一個Flowable,它基於每個專案延遲通過另一個Publisher對源Publisher的訂閱和排放。

2 delay圖解

圖1

圖2

3 delay測試用例

測試程式碼
    public void delay() {
        System.out.println(System.currentTimeMillis() + "|######delay#####,time");

        Flowable.just("item1").delay(3, TimeUnit.SECONDS)
                .subscribe(new Consumer<String>() {
                    @Override
                    public void accept(String s) throws Exception {
                        System.out.println(System.currentTimeMillis() + "|s =" + s);
                    }
                });
    }

輸出
I/System.out: 1538820032261|######delay#####,time
I/System.out: 1538820035263|s =item1

4 delay測試用例說明

上面的例子我沒用@Test單元測試方式寫,因為測試的時候發現無法列印結果,而是直接使用android的onclick呼叫,才能看出結果,句列印相隔3s以上。

這裡使用最簡單的一種說明,其他過載方法也是一樣的功能,有的增加delayError,處理error延遲,以及Scheduler作為延遲排程。