Rxjava从入门到精通:创建 *** 作符之:just(),From(),create

Rxjava从入门到精通:创建 *** 作符之:just(),From(),create,第1张

1: create() *** 作符
private static void testCreate() {
        Observable.create(new ObservableOnSubscribe() {
            @Override
            public void subscribe(ObservableEmitter emitter) throws Throwable {
                for (int i = 0; i < 10; i++) {
                    System.out.println("emitter发射value数据:" + i);
                    emitter.onNext("value=" + i);
                }
                emitter.onComplete();
            }
        }).subscribe(new Consumer() {
            @Override
            public void accept(Object o) throws Throwable {
                System.out.println(o);
            }
        });
}


打印结果:

emitter发射value数据:0
value=0
emitter发射value数据:1
value=1
emitter发射value数据:2
value=2
emitter发射value数据:3
value=3
emitter发射value数据:4
value=4
emitter发射value数据:5
value=5
emitter发射value数据:6
value=6
emitter发射value数据:7
value=7
emitter发射value数据:8
value=8
emitter发射value数据:9
value=9 
1.1 :create() *** 作符总结 

1)创建一个Observable,调用Observable.create();ObservableEmitter是事件的发送器,可以发送多个onNext()方法;一旦发送onComplete(),onError()事件之后,后续的事件将不会再发送;

2)创建一个Observer;里面有四个方法:onSubscribe(),onNext(),onError(),onComplete();

3)创建订阅关系 :Observable.subscribe(observer);

4) Observable.create() 创建 ObservableCreate被观察者 *** 作符
 

2:just() *** 作符 2.1 :just *** 作符作用:快速创建并发送事件

1)快速创建1个被观察者对象(Observable

2)发送事件的特点:直接发送 传入的事件

3)Observable.just() 创建 ObservableJust被观察者 *** 作符

2.2:使用示例
private static void testJust() {
        Observable.just(1,2,3,4,5,6,7,8,9)
                .subscribe(new Observer() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        System.out.println("use Subscribe connect Observable and 
                                                              Observer");
                    }

                    @Override
                    public void onNext(Integer integer) {
                        System.out.println("Next event" + integer + " response");
                    }

                    @Override
                    public void onError(Throwable e) {
                        System.out.println("Error event response");
                    }

                    @Override
                    public void onComplete() {
                        System.out.println("Complete event response");
                    }
                });
  }




打印结果:

use Subscribe connect Observable and Observer
Next event1 response
Next event2 response
Next event3 response
Next event4 response
Next event5 response
Next event6 response
Next event7 response
Next event8 response
Next event9 response
Complete event response
2.3 :just() *** 作符拓展

just(T item) ------这个代码可以引出思考,T 可以是某个业务逻辑返回结果,那么就可以有下面的场景。

1)类似Android网络访问,rxjava能不能直接把helloMicky()放在非主线程访问

2)而把subscribe放在主线程访问呢?这样就解决了android繁琐的callback问题了

3:fromArray() *** 作符 3.1: fromArray() *** 作符特点

1)快速创建1个被观察者对象(Observable

2)发送事件的特点:直接发送 传入的数组数据

3)实现数组的遍历

4)Observable.fromArray() 创建 ObservableFromArray *** 作符对象

3.2 :代码解析
private static void tesrArray() {
        Integer[] items = {0,1,2,3,4,5};
        Observable.fromArray(items)
                .subscribe(new Observer() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        System.out.println("use Subscribe connect Observable and Observer");
                    }

                    @Override
                    public void onNext(Integer integer) {
                        System.out.println("Next event" + integer + " response");
                    }

                    @Override
                    public void onError(Throwable e) {
                        System.out.println("Error event response");
                    }

                    @Override
                    public void onComplete() {
                        System.out.println("Complete event response");
                    }
                });
    }



打印结果:

use Subscribe connect Observable and Observer
Next event0 response
Next event1 response
Next event2 response
Next event3 response
Next event4 response
Next event5 response
Complete event response
4:fromIterable() *** 作符 4.1: fromIterable() *** 作符特点

1)快速创建1个被观察者对象(Observable

2) 发送事件的特点:直接发送 传入的集合List数据

3)集合元素遍历

4)Observable.fromIterable()创建

4.2 :代码示例
 private static void testIterable() {
        List items = new ArrayList<>();
        for (int i = 0; i < 6; i++) {
            items.add(i);
        }
        Observable.fromIterable(items)
                .subscribe(new Observer() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        System.out.println("use Subscribe connect Observable and Observer");
                    }

                    @Override
                    public void onNext(Integer integer) {
                        System.out.println("Next event" + integer + " response");
                    }

                    @Override
                    public void onError(Throwable e) {
                        System.out.println("Error event response");
                    }

                    @Override
                    public void onComplete() {
                        System.out.println("Complete event response");
                    }
                });
    }


打印结果
  use Subscribe connect Observable and Observer
Next event0 response
Next event1 response
Next event2 response
Next event3 response
Next event4 response
Next event5 response
Complete event response

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/724477.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-26
下一篇 2022-04-26

发表评论

登录后才能评论

评论列表(0条)