RxAndroid 2.0.x --- 基础篇

Gradle配置:

    compile 'io.reactivex.rxjava2:rxjava:2.0.1'
    compile 'io.reactivex.rxjava2:rxandroid:2.0.1'

关键词

  • Observable
//普通订阅
    public void demo1(View view) {
        Observable.create(new ObservableOnSubscribe() {
            @Override
            public void subscribe(ObservableEmitter e) throws Exception {
                e.onNext(1);
            }
        }).subscribe(new Observer() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(Integer value) {
                        Log.d(TAG, value + "");
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
 //不同线程订阅
    public void demo2(View view) {
        Observable.create(new ObservableOnSubscribe() {
            @Override
            public void subscribe(ObservableEmitter e) throws Exception {

                Log.d(TAG, Thread.currentThread().getName());
                e.onNext("");
            }
        }).subscribeOn(Schedulers.io())//第一次生效
                .observeOn(AndroidSchedulers.mainThread())//每一次生效覆盖前一次
                .subscribe(new Consumer() {
                    @Override
                    public void accept(String s) throws Exception {
                        Log.d(TAG, Thread.currentThread().getName());
                    }
                });
    }
    //转换map
    public void demo3(View view) {
        Observable.create(new ObservableOnSubscribe() {
            @Override
            public void subscribe(ObservableEmitter emitter) throws Exception {
                emitter.onNext(123);
            }
        }).map(new Function() {
            @Override
            public String apply(Integer integer) throws Exception {
                return String.valueOf(integer);
            }
        }).subscribe(new Consumer() {
            @Override
            public void accept(String s) throws Exception {
                Log.d(TAG, s);
            }
        });

    }
//flatmap  无序 把1个Observable 转换另一个类型Observable
    public void demo4(View view) {
        Observable.create(new ObservableOnSubscribe() {
            @Override
            public void subscribe(ObservableEmitter emitter) throws Exception {
                emitter.onNext(1);
                emitter.onNext(2);
                emitter.onNext(3);
                emitter.onNext(4);
                emitter.onComplete();
                Log.d(TAG, "subscribe=" + Thread.currentThread().getName());
            }
        }).flatMap(new Function>() {
            @Override
            public ObservableSource apply(Integer integer) throws Exception {
                Log.d(TAG, "apply=" + integer);
                Log.d(TAG, "apply=" + Thread.currentThread().getName());
                return Observable.just(integer % 2 == 0).delay((integer % 2) * 10, TimeUnit.SECONDS);
            }
        }).subscribeOn(Schedulers.newThread()).observeOn(Schedulers.newThread())
                .subscribe(new Consumer() {
                    @Override
                    public void accept(Boolean s) throws Exception {
                        Log.d(TAG, "accept=" + s);
                        Log.d(TAG, "accept=" + Thread.currentThread().getName());
                    }
                });
    }
 // concatMap 有许 其他同 flatmap
    public void demo5(View view) {
        Observable.create(new ObservableOnSubscribe() {
            @Override
            public void subscribe(ObservableEmitter emitter) throws Exception {
                emitter.onNext(1);
                emitter.onNext(2);
                emitter.onNext(3);
                emitter.onNext(4);
                emitter.onComplete();
                Log.d(TAG, "subscribe=" + Thread.currentThread().getName());
            }
        }).concatMap(new Function>() {
            @Override
            public ObservableSource apply(Integer integer) throws Exception {
                Log.d(TAG, "apply=" + integer);
                Log.d(TAG, "apply=" + Thread.currentThread().getName());
                return Observable.just(integer % 2 == 0).delay((integer % 2) * 10, TimeUnit.SECONDS);
            }
        }).subscribeOn(Schedulers.newThread()).observeOn(Schedulers.newThread())
                .subscribe(new Consumer() {
                    @Override
                    public void accept(Boolean s) throws Exception {
                        Log.d(TAG, "accept=" + s);
                        Log.d(TAG, "accept=" + Thread.currentThread().getName());
                    }
                });
    }
 //zip 打包Observable 个数等于 少的那个
    public void demo6(View view) {
        Observable observable1 = Observable.create(new ObservableOnSubscribe() {
            @Override
            public void subscribe(ObservableEmitter emitter) throws Exception {
                emitter.onNext(1);
                emitter.onNext(2);
                emitter.onNext(3);
                emitter.onNext(4);
                emitter.onNext(5);
            }
        });
        Observable observable2 = Observable.create(new ObservableOnSubscribe() {
            @Override
            public void subscribe(ObservableEmitter emitter) throws Exception {
                emitter.onNext("1");
                emitter.onNext("2");
                emitter.onNext("3");
            }
        });
        Observable.zip(observable1, observable2, new BiFunction() {
            @Override
            public Boolean apply(Integer integer, String s) throws Exception {
                Log.d(TAG, "apply=" + integer + "==" + s);
                return false;
            }
        }).subscribe(new Consumer() {
            @Override
            public void accept(Boolean aBoolean) throws Exception {
                Log.d(TAG, "accept=" + aBoolean);
            }
        });

    }
//背压  原来是上流发送过多的事件  下游处理不了
    //这里背压 就是下游告诉上游 我要多少个 其他多的就舍弃或者存最新的 或者报错
    public void demo7(View view) {
        Flowable.create(new FlowableOnSubscribe() {
            @Override
            public void subscribe(FlowableEmitter emitter) throws Exception {
                Log.d(TAG, "emit 1");
                emitter.onNext(1);
                Log.d(TAG, "emit 2");
                emitter.onNext(2);
                Log.d(TAG, "emit 3");
                emitter.onNext(3);
                Log.d(TAG, "emit complete");
                emitter.onComplete();
            }
        }, BackpressureStrategy.ERROR).subscribe(new Subscriber() {

            @Override
            public void onSubscribe(Subscription s) {
                Log.d(TAG, "onSubscribe");
            }

            @Override
            public void onNext(Integer integer) {
                Log.d(TAG, "onNext: " + integer);

            }

            @Override
            public void onError(Throwable t) {
                Log.w(TAG, "onError: ", t);
            }

            @Override
            public void onComplete() {
                Log.d(TAG, "onComplete");
            }
        });
    }

你可能感兴趣的:(RxAndroid 2.0.x --- 基础篇)