Rxjava 创建操作符(三)

操作符入门:

RxJava 操作符的类型分为创建操作符、变换操作符、过滤操作符、组合操作符、错误处理操作符、辅助操作符、条件和布尔操作符、算数和聚合操作符及连接操作符等。而这些操作符类型下又有很多操作,每个操作又有很多变体。

常用的创建操作符:

常用的有 create、just、from
不常用的有 defer、range、interval、start、repeat 和 timer

一、just操作符方法

创建发送指定值的Observerble,just只是简单的原样发射,将数组或Iterable当做单个数据。如果传递的值为null,则发送的Observable的值为null。参数最多为9个。

Observable myObservable = Observable.just("just1","just1","just1","just1","just1","just1","just1","just1","just1","just1");
 
        Subscriber mySubscriber = new Subscriber() {
            @Override
            public void onNext(String s) {
                System.out.println(s);
            }
 
            @Override
            public void onCompleted() {
                System.out.println("onCompleted.................");
            }
 
            @Override
            public void onError(Throwable e) {
                System.out.println("onError....................");
            }
        };
 
        myObservable.subscribe(mySubscriber);

二、from方法
将数据转换成为Observables,而不是需要混合使用Observables和其它类型的数据。

String[]items = {"just1","just1","just1","just1","just1","just1"};
 
        Observable myObservable = Observable.from(items);
 
        Subscriber mySubscriber = new Subscriber() {
            @Override
            public void onNext(String s) {
                System.out.println(s);
            }
 
            @Override
            public void onCompleted() {
                System.out.println("onCompleted.................");
            }
 
            @Override
            public void onError(Throwable e) {
                System.out.println("onError....................");
            }
        };
 
        myObservable.subscribe(mySubscriber);

三、repeat方法

1.repeat()重复地执行某个操作,如果不传递参数,结果将会被无限地重复执行,默认在trampoline调度器上执行,该方法为非静态方法,不可以直接通过Observable来调用

 String[]items = {"just1","just2","just3","just4","just5","just6"};
 
        Observable myObservable = Observable.from(items).repeat();
 
 
        Subscriber mySubscriber = new Subscriber() {
            @Override
            public void onNext(String s) {
                System.out.println(s);
            }
 
            @Override
            public void onCompleted() {
                System.out.println("onCompleted.................");
            }
 
            @Override
            public void onError(Throwable e) {
                System.out.println("onError....................");
            }
        };
 
        myObservable.subscribe(mySubscriber);

2.repeat()如果传入数字类型的参数,则重复地执行指定次数的某个操作,默认在trampoline调度器上执行,不可以直接通过Observable来调用

String[]items = {"just1","just2","just3","just4","just5","just6"};
 
        Observable myObservable = Observable.from(items).repeat(2);
 
 
        Subscriber mySubscriber = new Subscriber() {
            @Override
            public void onNext(String s) {
                System.out.println(s);
            }
 
            @Override
            public void onCompleted() {
                System.out.println("onCompleted.................");
            }
 
            @Override
            public void onError(Throwable e) {
                System.out.println("onError....................");
            }
        };
 
        myObservable.subscribe(mySubscriber);

3.repeatWhen()不是缓存和重放原始Observable的数据序列,而是有条件的重新订阅和发射原来的Observable,当Observable中的call()方法中调用了重复执行的代码时,onNext()将会被重复执行。如果该方法执行后返回void,则结束执行

 final String[]items = {"just1","just2","just3","just4","just5","just6"};
 
        Observable myObservable = Observable.from(items).repeatWhen(new Func1, Observable>() {
            @Override
            public Observable call(Observable observable) {
                return observable.delay(5, TimeUnit.SECONDS);
            }
        });
 
 
        Subscriber mySubscriber = new Subscriber() {
            @Override
            public void onNext(String s) {
                System.out.println("onNext.................."+s);
            }
 
            @Override
            public void onCompleted() {
                System.out.println("onCompleted.................");
            }
 
            @Override
            public void onError(Throwable e) {
                System.out.println("onError....................");
            }
        };
 
        myObservable.subscribe(mySubscriber);

你可能感兴趣的:(Rxjava 创建操作符(三))