RxJava basic processing sequence (old)

Reactive study articles

I want to understand Reactive but I don't know anything I will describe how the sample code of Reactive Programming With RxJava works behind the scenes.

Since it's a 2016 book, it uses old APIs that are deprecated, such as Observable.create.

RxJava

Simple characters

store-uml-RxJava.png

store-uml-RxJava_2.png

simple

fun main(args: Array<String>) {
    Observable.create<String> { s ->
        s.onNext("one")
        s.onCompleted()
    }.subscribe { s -> println("VALUE -> $s") }
}

store-uml-RxJava_sequence1.png

doOnNext

fun main(args: Array<String>) {
    Observable.create<String> { s ->
        s.onNext("one")
        s.onCompleted()
    }.doOnNext { i -> println("doOnNext $i") }
            .subscribe { s -> println("VALUE -> $s") }
}

store-uml-RxJava_sequence2.png

The process in which orange is created by the doOnNext association You can see that it is acting like a proxy

map

fun main(args: Array<String>) {
    Observable.create<String> { s ->
        s.onNext("one")
        s.onCompleted()
    }.map { "hello $it" }
            .subscribe { s -> println("VALUE -> $s") }
}

store-uml-RxJava_sequence3.png

Almost the same behavior as doOnNext

Publisher from another Thread

fun main(args: Array<String>) {
    println(Thread.currentThread())
    Observable.create<String> { s ->
        Thread {
            println(Thread.currentThread())
            s.onNext("one")
            s.onNext("two")
            s.onCompleted()
        }.start()
    }.subscribe { s -> println("VALUE -> $s by ${Thread.currentThread()}") }
}

//Output result
Thread[main,5,main]
Thread[Thread-0,5,main]
VALUE -> one by Thread[Thread-0,5,main]
VALUE -> two by Thread[Thread-0,5,main]
VALUE -> three by Thread[Thread-0,5,main]

store-uml-RxJava_sequence4.png

If it is published in another thread, it will be subscribed in that thread.

Recommended Posts

RxJava basic processing sequence (old)
Memorandum (Ruby: Basic grammar: Iterative processing)
Basic processing flow of java Stream
[Java] Exception types and basic processing