The advanced RxJava of kotlin is RxJava2

Keywords: Maven Mobile

RxJava advanced RxJava2

Recently, I used RxJava when I was working on a project. After learning about RxJava2, I decided to upgrade RxJava of the project to RxJava2. RxJava 2 is completely rewritten based on the reactive streams specification. The specification itself has evolved from RxJava, so you don't need to learn RxJava before using RxJava 2.

  • Maven address and base package
  • Observer mode
  • kotlin code
  • summary

Maven address and base package

  • Rxjava
// RxKotlin and RxAndroid
    compile "io.reactivex:rxkotlin:$rx_kotlin_version"
    compile "io.reactivex:rxandroid:${rx_android_version}"
  • RxJava2
 io.reactivex.rxjava2:rxjava:2.1.8
 io.reactivex.rxjava2:rxandroid:2.0.1
 io.reactivex.rxjava2:rxkotlin:2.2.0`

Observer mode

It can be seen from the figure that RxJava supports two types of observable subscription: Observer and subscriber, while RxJava2 separates the type of observable subscription subscriber from that of Flowable subscription subscriber.

kotlin code

Next, I will introduce the code difference between the Rxjava adopted in the project and the advanced Rxjava2

  • Rxjava

In user module implementation class

class UserServiceImpl :UserService{


    override fun register(mobile: String, pwd: String, verifyCode: String): Observable<Boolean> {
      return  Observable.just(true)
    }

}

Handle user registration at the business level

 val userService=UserServiceImpl()
 userService.register(username,password,verityCode)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(object :Subscriber<Boolean>{
                    override fun onComplete() {
                    }

                    override fun onNext(t: Boolean) {
                        mView.result("login was successful")
                    }

                    override fun onError(e: Throwable) {

                    }

                })
  • Rxjava2

In user module implementation class

class UserServiceImpl :UserService{

    override fun register(mobile: String, pwd: String, verifyCode: String): Flowable<Boolean> {
        return  Flowable.just(true)
}

}

Handle user registration at the business level

 val userService=UserServiceImpl()
userService.register(username,password,verityCode)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(object :Subscriber<Boolean>{
                    override fun onComplete() {

                    }

                    override fun onSubscribe(s: Subscription?) {

                    }

                    override fun onNext(t: Boolean?) {
                        mView.result("login was successful")
                    }

                    override fun onError(t: Throwable?) {

                    }

                })

summary

RxJava and RxJava2 are essentially an upgrade. The back pressure refers to a strategy that tells the observed to reduce the sending speed when the observed sends events much faster than the observer's processing speed in asynchronous scenes. RxJava2 solves the back pressure problem perfectly through Flowable, but the design of solving the back pressure problem in RxJava is not perfect.

Posted by matjaz on Mon, 04 May 2020 17:26:28 -0700