THE BEGINNER’S GUIDE TO
RXJAVA
2016.07.21
HIROSHI KUROKAWA (@HYDRAKECAT)
Slide 2
Slide 2 text
WHAT’S RXJAVA
Slide 3
Slide 3 text
WHAT’S RXJAVA
▸ Handle a sequence of data over time as a stream
▸ Instead of gathering them, responding to each of them
Slide 4
Slide 4 text
WHAT’S RXJAVA
▸ Handle a sequence of data over time as a stream
▸ Instead of gathering them, responding to each of them
1 2 3 4 5 6
Slide 5
Slide 5 text
WHAT’S RXJAVA
▸ Handle a sequence of data over time as a stream
▸ Instead of gathering them, responding to each of them
1 2 3 4 5 6
if ( % 2 == 0)
x x
square( )
Slide 6
Slide 6 text
WHAT’S RXJAVA
▸ Handle a sequence of data over time as a stream
▸ Instead of gathering them, responding to each of them
1 2 3 4 5 6
if ( % 2 == 0)
x x
square( )
4 16 36
Slide 7
Slide 7 text
WHAT’S RXJAVA
▸ Handle a sequence of data over time as a stream
▸ Instead of gathering them, responding to each of them
Observable.just(1, 2, 3, 4, 5, 6)
.filter(x -> x % 2 == 0)
.map(x -> x * x)
.subscribe(System.out::println);
WHY DIFFICULT?
1. Many operators
2. Some complicated concepts
Slide 15
Slide 15 text
WHY DIFFICULT?
1. Many operators
2. Some complicated concepts
‣ Hot/Cold observable
Slide 16
Slide 16 text
WHY DIFFICULT?
1. Many operators
2. Some complicated concepts
‣ Hot/Cold observable
‣ Scheduler
Slide 17
Slide 17 text
WHY DIFFICULT?
1. Many operators
2. Some complicated concepts
‣ Hot/Cold observable
‣ Scheduler
‣ Subscription
Slide 18
Slide 18 text
WHY DIFFICULT?
1. Many operators
2. Some complicated concepts
‣ Hot/Cold observable
‣ Scheduler
‣ Subscription
‣ Subject
Slide 19
Slide 19 text
WHY DIFFICULT?
1. Many operators
2. Some complicated concepts
‣ Hot/Cold observable
‣ Scheduler
‣ Subscription
‣ Subject
‣ Back pressure
Slide 20
Slide 20 text
ADVICE 1: READ THE DOCUMENT
ReactiveX/RxJava Wiki
https://github.com/ReactiveX/RxJava/wiki
Slide 21
Slide 21 text
ADVISE 2: GET USED TO OPERATORS
▸ Play with Rx operators
▸ Understand how operators work with your heart
Slide 22
Slide 22 text
ADVISE 2: GET USED TO OPERATORS
▸ Play with Rx operators
▸ Understand how operators work with your heart
▸ RxJS
▸ http://reactivex.io/rxjs/
▸ JSFiddle
▸ https://jsfiddle.net
SOME HINTS
‣ Are you interested in more than one Observables?
→ Combining / Conditional
Slide 26
Slide 26 text
SOME HINTS
‣ Are you interested in more than one Observables?
→ Combining / Conditional
‣ Or just one Observable?
→ Transforming / Filtering
Slide 27
Slide 27 text
SOME HINTS
‣ Are you interested in more than one Observables?
→ Combining / Conditional
‣ Or just one Observable?
→ Transforming / Filtering
‣ Are you creating an Observable?
→ Async / Observable Creation
Slide 28
Slide 28 text
SOME HINTS
‣ Are you interested in more than one Observables?
→ Combining / Conditional
‣ Or just one Observable?
→ Transforming / Filtering
‣ Are you creating an Observable?
→ Async / Observable Creation
A Decision Tree of Observable Operators
(http://reactivex.io/documentation/operators.html)
Slide 29
Slide 29 text
EXAMPLE 1: NETWORK CALL
Slide 30
Slide 30 text
EXAMPLE 1: NETWORK CALL
‣ Wait until both the network calls return and then combine
the responses
Slide 31
Slide 31 text
EXAMPLE 1: NETWORK CALL
‣ Wait until both the network calls return and then combine
the responses
‣ Use zipWith()
Slide 32
Slide 32 text
EXAMPLE 1: NETWORK CALL
‣ Wait until both the network calls return and then combine
the responses
‣ Use zipWith()
service.getDataA()
.zipWith(service.getDataB(), Pair::new)
.subscribeOn(Schedulers.io())
.subscribe(subscriber);
Slide 33
Slide 33 text
EXAMPLE 1: NETWORK CALL
‣ Chain a sequence of network calls
Slide 34
Slide 34 text
EXAMPLE 1: NETWORK CALL
‣ Chain a sequence of network calls
‣ Use flatMap()
Slide 35
Slide 35 text
EXAMPLE 1: NETWORK CALL
‣ Chain a sequence of network calls
‣ Use flatMap()
service.getDataA()
.flatMap((a) -> service.getDataB().map((b) -> new Pair<>(a, b)))
.subscribeOn(Schedulers.io())
.subscribe(subscriber);
Slide 36
Slide 36 text
EXAMPLE 2: HANDLE UI EVENTS
Slide 37
Slide 37 text
EXAMPLE 2: HANDLE UI EVENTS
‣ Enable a button only when all the input fields are valid
Slide 38
Slide 38 text
EXAMPLE 2: HANDLE UI EVENTS
‣ Enable a button only when all the input fields are valid
‣ Use combineLatest()
Slide 39
Slide 39 text
EXAMPLE 2: HANDLE UI EVENTS
‣ Enable a button only when all the input fields are valid
‣ Use combineLatest()
Observable.combineLatest(
inputValidators,
booleans -> Arrays.stream(booleans).allMatch(b -> (Boolean) b)
).subscribe(subscriber);
Slide 40
Slide 40 text
EXAMPLE 2: HANDLE UI EVENTS
‣ Pair the adjacent two events
a b c d e f
a, b c, d e, f
b, c d, e
Slide 41
Slide 41 text
EXAMPLE 2: HANDLE UI EVENTS
‣ Pair the adjacent two events
‣ Use scan()
a b c d e f
a, b c, d e, f
b, c d, e
Slide 42
Slide 42 text
EXAMPLE 2: HANDLE UI EVENTS
‣ Pair the adjacent two events
‣ Use scan()
Observable.just("a", "b", "c", "d", "e", "f")
.scan(new Pair<>(null, null), ((pair, s) -> new Pair<>(pair.second, s)))
.skip(2)
.subscribe(subscriber);
Slide 43
Slide 43 text
ADVICE 4: TAKE ADVANTAGE OF EXISTING RX LIBRARIES
Slide 44
Slide 44 text
ADVICE 4: TAKE ADVANTAGE OF EXISTING RX LIBRARIES
‣ Don’t create your Observable with Observable.create()
Slide 45
Slide 45 text
ADVICE 4: TAKE ADVANTAGE OF EXISTING RX LIBRARIES
‣ Don’t create your Observable with Observable.create()
‣ Use libraries instead
Slide 46
Slide 46 text
ADVICE 4: TAKE ADVANTAGE OF EXISTING RX LIBRARIES
‣ Don’t create your Observable with Observable.create()
‣ Use libraries instead
‣ Network calls: Retrofit
‣ SQLite: SQLBrite
‣ UI events: RxBinding
Slide 47
Slide 47 text
ADVICE 5: HAVE FUN!
(http://slides.com/robwormald/everything-is-a-stream#/)