RxJava(RxAndroid)_Observable的创建

中文地址:
https://mcxiaoke.gitbooks.io/rxdocs/content/operators/Creating-Observables.html

Observable作为RxJava中使用的第一步就是创建它,以下是几种常见的创建方法

对应版本的依赖:
compile ‘io.reactivex.rxjava2:rxandroid:2.0.1’
compile ‘io.reactivex.rxjava2:rxjava:2.1.8’

Demo地址:https://github.com/baopengjian/RxAndroid

just

将一个或多个对象转换成发射这个或这些对象的一个Observable

 Observable.just("111").subscribe(new Observer() {
            @Override
            public void onSubscribe(@NonNull Disposable d) {
                Log.i(TAG, "onSubscribe -> ");
            }

            @Override
            public void onNext(@NonNull String s) {
                Log.i(TAG, "onNext -> s=" + s);
            }

            @Override
            public void onError(@NonNull Throwable e) {
                Log.i(TAG, "onError -> e=" + e);
            }

            @Override
            public void onComplete() {
                Log.i(TAG, "onComplete -> ");

            }
        });

from

将一个Iterable, 一个Future, 或者一个数组转换成一个Observable

   public void from() {
        Observable.fromArray(new String[]{"0", "1", "2"})
                .subscribe(new Consumer() {
                    @Override
                    public void accept(String o) throws Exception {
                        Log.i(TAG, "accept -> o =" + o);
                    }
                });
    }

create

使用一个函数从头创建一个Observable

   Observable.create(new ObservableOnSubscribe() {
            @Override
            public void subscribe(@NonNull ObservableEmitter emitter) throws Exception {
                emitter.onNext("111");
                emitter.onComplete();
            }
        }).subscribe(new Observer() {
           ...
        });

defer

只有当订阅者订阅才创建Observable;为每个订阅创建一个新的Observable

  Observable.defer(new Callable>() {
            @Override
            public ObservableSource call() throws Exception {
                return new ObservableSource() {

                    @Override
                    public void subscribe(@NonNull Observer observer) {
                        observer.onNext("111");
                        observer.onComplete();
                    }
                };
            }
        }).subscribe(new Observer() {
            ...
        });

rang

创建一个发射指定范围的整数序列的Observable

  Observable.range(1, 3).subscribe(new Consumer() {
            @Override
            public void accept(Integer integer) throws Exception {
                Log.i(TAG, "onNext -> integer =" + integer);
            }
        }, new Consumer() {
            @Override
            public void accept(Throwable throwable) throws Exception {
                Log.i(TAG, "onNext -> throwable =" + throwable);
            }
        });

interval

创建一个按照给定的时间间隔发射整数序列的Observable

Observable.interval(5, 3, TimeUnit.SECONDS)
                .subscribeWith(new DisposableObserver() {
                    @Override
                    public void onNext(@NonNull Long o) {
                        Log.i(TAG, "onNext -> o =" + o);
                        if (10 == o) {
                            dispose();
                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        dispose();
                    }

                    @Override
                    public void onComplete() {

                    }
                });


//        //从1到3,5秒后开始周期为3s的循环
//        Observable.intervalRange(1, 3, 5, 3, TimeUnit.SECONDS)
//                .subscribe(new Consumer() {
//                    @Override
//                    public void accept(Long aLong) throws Exception {
//                        Log.i(TAG, "onNext -> aLong =" + aLong);
//                    }
//                });

empty

创建一个什么都不做直接通知完成的Observable

 Observable.empty()
                .subscribe(new Observer() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        Log.i(TAG, "onSubscribe -> ");
                    }

                    @Override
                    public void onNext(@NonNull Object s) {
                        Log.i(TAG, "onNext -> s=" + s);
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        Log.i(TAG, "onError -> e=" + e);
                    }

                    @Override
                    public void onComplete() {
                        Log.i(TAG, "onComplete -> ");

                    }
                });
        //执行结果
        //RxCreateActivity: onSubscribe ->
        //RxCreateActivity: onComplete -> 
  

never

创建一个不发射任何数据的Observable

  Observable.never().subscribe(new Observer() {
            @Override
            public void onSubscribe(@NonNull Disposable d) {
                Log.i(TAG, "onSubscribe -> ");
            }

            @Override
            public void onNext(@NonNull Object s) {
                Log.i(TAG, "onNext -> s=" + s);
            }

            @Override
            public void onError(@NonNull Throwable e) {
                Log.i(TAG, "onError -> e=" + e);
            }

            @Override
            public void onComplete() {
                Log.i(TAG, "onComplete -> ");
            }
        });
        // RxCreateActivity: onSubscribe -> 
  

                            
                        
                    
                    
                    

你可能感兴趣的:(RxJava)