RxJava学习(一)之简单使用

一、简介

RxJava是一个响应式的编程,基于观察者模式

二、简单使用

1、无背压
   private fun initListener() {
        mRxJavaRecycleTest.onClick {
            Log.i(TAG,"==mRxJavaRecycleTest==")
            Observable.create<String> {
                emitter->
                emitter.onNext("无背压")
                emitter.onComplete()
            }.subscribe(object : Observer<String?> {
                override fun onComplete() {
                    Log.i(TAG,"==onComplete==")
                }

                override fun onSubscribe(d: Disposable) {
//                    d.dispose()
                }

                override fun onNext(t: String) {
                    Log.i(TAG,"==onNext=${t}=")
                }

                override fun onError(e: Throwable) {

                }
            })
        }
    }
2、有背压
   private fun initListener() {
        mRxJavaRecycleTest.onClick {
            Flowable.create<String>({
                emitter: FlowableEmitter<String> ->
                emitter.onNext("有背压")
                emitter.onComplete()
            },BackpressureStrategy.DROP)
                .subscribe(object : Subscriber<String?>{
                    override fun onComplete() {
                        Log.i(TAG,"==onComplete==")
                    }

                    override fun onSubscribe(s: Subscription?) {
                        s?.request(Long.MAX_VALUE)
                    }
                    override fun onNext(t: String?) {
                        Log.i(TAG,"==onNext=${t}=")
                    }
                    override fun onError(t: Throwable?) {

                    }
                })
        }
    }

三、冷和热Observable

cold Observable

观察者订阅了,才会开始执行发射数据流的代码。多个观察者之间事件是相互独立的。Observable的just、create、rang等等操作符都是冷的。

    private fun initListener() {
        mRxJavaRecycleTest.onClick {
             val observable = Observable.interval(200, TimeUnit.MILLISECONDS)
             observable.subscribe {
                 Log.i(TAG,"=hot=Consumer1=${it}=")
             }
            Thread.sleep(500)
            observable.subscribe {
                Log.i(TAG,"   =hot=Consumer2=${it}=")
            }
        }
    }

输出的结果:

=cold=Consumer1=0=
=cold=Consumer1=1=
=cold=Consumer1=2=
    =cold=Consumer2=0=
=cold=Consumer1=3=
    =cold=Consumer2=1=
=cold=Consumer1=4=
    =cold=Consumer2=2=
=cold=Consumer1=5=
    =cold=Consumer2=3=
=cold=Consumer1=6=
    =cold=Consumer2=4=
=cold=Consumer1=7=
    =cold=Consumer2=5=
hot Observable

无论有没有观察者订阅,事件始终都会发生,多个观察者进行订阅时,信息是共享的。

    private fun initListener() {
        mRxJavaRecycleTest.onClick {
             val observable = Observable.interval(200, TimeUnit.MILLISECONDS).publish()
             
            observable.connect()

             observable.subscribe {
                 Log.i(TAG,"=hot=Consumer1=${it}=")
             }
            Thread.sleep(500)
            observable.subscribe {
                Log.i(TAG,"   =hot=Consumer2=${it}=")
            }
        }
    }

测试结果:

=hot=Consumer1=0=
=hot=Consumer1=1=
=hot=Consumer1=2=
    =hot=Consumer2=2=
=hot=Consumer1=3=
    =hot=Consumer2=3=
=hot=Consumer1=4=
    =hot=Consumer2=4=
=hot=Consumer1=5=
    =hot=Consumer2=5=
=hot=Consumer1=6=
    =hot=Consumer2=6=
=hot=Consumer1=7=
    =hot=Consumer2=7=
=hot=Consumer1=8=
    =hot=Consumer2=8=

你可能感兴趣的:(Android)