RxJava2 Flowable delay delaySubscription(辅助操作符)

delay (辅助操作符)

目录

1 dealy接口 

2 delay图解

3 delay测试用例

4 delay测试用例说明

5 delaySubscription操作符


 

 

1 dealy接口 

<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

RxJava2 Flowable delay delaySubscription(辅助操作符)

图2

RxJava2 Flowable delay delaySubscription(辅助操作符)

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作为延迟调度。

 

 

5 delaySubscription操作符

delaySubscription和delay不同,delaySubscription是延迟订阅源Publisher,订阅延迟,对于冷Publisher自然也就延迟收到观察数据,但是对于热Publisher就不一样了,因为他在创建后就开始发射数据,不是非得被订阅了才发发射数据,所以延迟订阅热观察源可能会丢失订阅前被发射的数据。

Flowable<T>

delaySubscription(long delay, TimeUnit unit)

Returns a Flowable that delays the subscription to the source Publisher by a given amount of time.

返回一个Flowable,它将给订阅源的订阅延迟一段给定的时间。

Flowable<T>

delaySubscription(long delay, TimeUnit unit, Scheduler scheduler)

Returns a Flowable that delays the subscription to the source Publisher by a given amount of time, both waiting and subscribing on a given Scheduler.

返回一个Flowable,它将给订阅源的订阅延迟给定的时间,包括在给定的Scheduler上等待和订阅。

<U> Flowable<T>

delaySubscription(Publisher<U> subscriptionIndicator)

Returns a Flowable that delays the subscription to this Publisher until the other Publisher emits an element or completes normally.

返回一个Flowable,它会延迟对此Publisher的订阅,直到另一个Publisher发出元素或正常完成为止。

查看其它操作符

switchIfEmpty(条件操作符)

defaultIfEmpty(条件操作符)

debounce(过滤操作符)去重复操作

count(统计操作符)

contains(条件操作符)

concatWith(连接操作符)

concatMap(变换操作符)

collect & collectInto

cast

cache

buffer