THE BEGINNER’S GUIDE TO
RXJAVA
2016.09.16
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
GUIDE 1: READ THE DOCUMENT
ReactiveX/RxJava Wiki
https://github.com/ReactiveX/RxJava/wiki
Introduction to Rx
http://www.introtorx.com/
101 Rx Samples
http://rxwiki.wikidot.com/101samples
Slide 21
Slide 21 text
GUIDE 2: GET USED TO OPERATORS
▸ Play with Rx operators
▸ Understand how operators work with your heart
Slide 22
Slide 22 text
GUIDE 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 29
Slide 29 text
SOME HINTS
‣ Are you interested in more than one Observables?
→ Combining / Conditional
‣ Or just one Observable?
→ Transforming / Filtering
Slide 30
Slide 30 text
SOME HINTS
‣ Are you interested in more than one Observables?
→ Combining / Conditional
‣ Or just one Observable?
→ Transforming / Filtering
‣ Are you creating an Observable?
→ Creating
Slide 31
Slide 31 text
SOME HINTS
‣ Are you interested in more than one Observables?
→ Combining / Conditional
‣ Or just one Observable?
→ Transforming / Filtering
‣ Are you creating an Observable?
→ Creating
A Decision Tree of Observable Operators
(http://reactivex.io/documentation/operators.html)
Slide 32
Slide 32 text
EXAMPLE 1: NETWORK CALL
Slide 33
Slide 33 text
EXAMPLE 1: NETWORK CALL
‣ Wait until both the network calls return and then combine
the responses
Slide 34
Slide 34 text
EXAMPLE 1: NETWORK CALL
‣ Wait until both the network calls return and then combine
the responses
‣ Use zipWith()
Slide 35
Slide 35 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 36
Slide 36 text
EXAMPLE 1: NETWORK CALL
‣ Chain a sequence of network calls
Slide 37
Slide 37 text
EXAMPLE 1: NETWORK CALL
‣ Chain a sequence of network calls
‣ Use flatMap()
Slide 38
Slide 38 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 39
Slide 39 text
EXAMPLE 2: HANDLE UI EVENTS
Slide 40
Slide 40 text
EXAMPLE 2: HANDLE UI EVENTS
‣ Enable a button only when all the input fields are valid
Slide 41
Slide 41 text
EXAMPLE 2: HANDLE UI EVENTS
‣ Enable a button only when all the input fields are valid
‣ Use combineLatest()
Slide 42
Slide 42 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 43
Slide 43 text
EXAMPLE 2: HANDLE UI EVENTS
‣ Periodically do something while list view is idling
Slide 44
Slide 44 text
EXAMPLE 2: HANDLE UI EVENTS
‣ Periodically do something while list view is idling
‣ Use takeUntil()
Slide 45
Slide 45 text
EXAMPLE 2: HANDLE UI EVENTS
‣ Periodically do something while list view is idling
‣ Use takeUntil()
ConnectableObservable idling = RxAbsListView.scrollEvents(list)
.map(event -> event.scrollState() == SCROLL_STATE_IDLE)
.distinctUntilChanged()
.startWith(true)
.publish();
idling.connect();
idling
.filter(b -> b)
.subscribe(aBool ->
Observable.interval(1, TimeUnit.SECONDS)
.takeUntil(idling.filter(b -> !b))
.subscribe(aLong -> appendLog(".")));
Slide 46
Slide 46 text
GUIDE 4: TAKE ADVANTAGE OF EXISTING RX LIBRARIES
‣ Don’t create your Observable with Observable.create()
Slide 47
Slide 47 text
GUIDE 4: TAKE ADVANTAGE OF EXISTING RX LIBRARIES
‣ Don’t create your Observable with Observable.create()
‣ Use libraries instead
Slide 48
Slide 48 text
GUIDE 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 49
Slide 49 text
GUIDE 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
‣ Or use Observable.from~()
‣ fromCallble()
‣ fromEmitter()
‣ etc.
Slide 50
Slide 50 text
GUIDE 5: HAVE FUN!
(http://slides.com/robwormald/everything-is-a-stream#/)
Slide 51
Slide 51 text
TEXT
SUMMARY
▸ Guide 1: Read the documents
▸ Guide 2: Get used to the operators
▸ Guide 3: Know the operator categories
▸ Guide 4: Take advantage of the existing libraries
▸ Guide 5: Have Fun