RxJava操作符(9)-连接

Connect

Publish操作符将原有的Observable转化为ConnectableObservable,ConnectableObservable在被订阅的时候不会发射数据,而是在调用Connect操作符时才发射数据。Connect操作符会生成Subscription对象,如果想终止数据的发射,调用unsubscribe即可。如果一个Observable没有订阅着订阅它,可以使用Connect操作符让Observable
发射数据。

原理图如下:


Connect操作符使用如下:

    private Action1<Long> mAction1;
    private Action1<Long> mAction2;

    @Override
    protected void createObservable() {
        super.createObservable();
        isConnect = true;
        createAction1();
        createAction2();
        mObservable = Observable.interval(1, TimeUnit.SECONDS)
                .observeOn(AndroidSchedulers.mainThread())
                .publish();
        mObservable.subscribe(mAction1);
    }

    private void createAction1() {
        mAction1 = new Action1<Long>() {
            @Override
            public void call(Long integer) {
                displayLogcat("createAction1 call integer = " + integer);
                if (integer == 3) {
                    mObservable.subscribe(mAction2);
                }
            }
        };
    }

    private void createAction2() {
        mAction2 = new Action1<Long>() {
            @Override
            public void call(Long integer) {
                displayLogcat("createAction2 call integer = " + integer);
            }
        };
    }


运行代码,结果如下:

RefCount

RefCount操作符是将一个ConnectableObservable转换为一个普通的Observable,并通过订阅者的订阅,将数据发送出去。

原理图如下:


RefCount操作符使用如下:

    @Override
    protected void createObservable() {
        super.createObservable();
        mObservable = Observable.just(1, 2, 3, 4, 5, 6, 7, 8, 9)
                .publish()
                .refCount();
    }


运行代码,结果如下:

Replay

Replay操作符返回一个ConnectableObservable对象并且可以缓存其发射过的数据,这样即使有订阅者在其发射数据之后进行订阅也能收到其之前发射过的数据。不过使用Replay操作符我们最好还是限定其缓存的大小,否则缓存的数据太多了可会占用很大的一块内存。对缓存的控制可以从空间和时间两个方面来实现。

原理图如下:


Replay操作符使用如下:

    private Action1<Long> mAction1;
    private Action1<Long> mAction2;

    @Override
    protected void createObservable() {
        super.createObservable();
        isConnect = true;
        createAction1();
        createAction2();
        mObservable = Observable.interval(1, TimeUnit.SECONDS)
                .observeOn(AndroidSchedulers.mainThread())
                .replay(3);
//                .replay(3, TimeUnit.SECONDS);
        mObservable.subscribe(mAction1);
    }

    private void createAction1() {
        mAction1 = new Action1<Long>() {
            @Override
            public void call(Long integer) {
                displayLogcat("createAction1 call integer = " + integer);
                if (integer == 3) {
                    mObservable.subscribe(mAction2);
                }
            }
        };
    }

    private void createAction2() {
        mAction2 = new Action1<Long>() {
            @Override
            public void call(Long integer) {
                displayLogcat("createAction2 call integer = " + integer);
            }
        };
    }


运行代码,结果如下:




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值