Slide 1

Slide 1 text

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);

Slide 8

Slide 8 text

WHY DIFFICULT?

Slide 9

Slide 9 text

WHY DIFFICULT? 1. Many operators

Slide 10

Slide 10 text

WHY DIFFICULT? 1. Many operators all() amb() ambWith() and() asyncAction() asyncFunc() averageDouble() averageFloat() averageInteger() averageLong() buffer() cache() cast() chunkify() collect() combineLatest() concat() concatMap() concatWith()

Slide 11

Slide 11 text

WHY DIFFICULT? 1. Many operators all() amb() ambWith() and() asyncAction() asyncFunc() averageDouble() averageFloat() averageInteger() averageLong() buffer() cache() cast() chunkify() collect() combineLatest() concat() concatMap() concatWith() connect() contains() count() countLong() create() debounce() defaultIfEmpty() defer() deferFuture() deferCancellableFuture() delay() dematerialize() distinct() distinctUntilChanged() doOnCompleted() doOnEach() doOnError() doOnNext() doOnRequest()

Slide 12

Slide 12 text

WHY DIFFICULT? 1. Many operators all() amb() ambWith() and() asyncAction() asyncFunc() averageDouble() averageFloat() averageInteger() averageLong() buffer() cache() cast() chunkify() collect() combineLatest() concat() concatMap() concatWith() connect() contains() count() countLong() create() debounce() defaultIfEmpty() defer() deferFuture() deferCancellableFuture() delay() dematerialize() distinct() distinctUntilChanged() doOnCompleted() doOnEach() doOnError() doOnNext() doOnRequest() doOnSubscribe() doOnTerminate() doOnUnsubscribe() doWhile() elementAt() elementAtOrDefault() empty() error() exists() filter() finallyDo() first() firstOrDefault() flatMap() flatMapIterable() forEach() forIterable() from() fromAction()

Slide 13

Slide 13 text

WHY DIFFICULT? 1. Many operators all() amb() ambWith() and() asyncAction() asyncFunc() averageDouble() averageFloat() averageInteger() averageLong() buffer() cache() cast() chunkify() collect() combineLatest() concat() concatMap() concatWith() connect() contains() count() countLong() create() debounce() defaultIfEmpty() defer() deferFuture() deferCancellableFuture() delay() dematerialize() distinct() distinctUntilChanged() doOnCompleted() doOnEach() doOnError() doOnNext() doOnRequest() doOnSubscribe() doOnTerminate() doOnUnsubscribe() doWhile() elementAt() elementAtOrDefault() empty() error() exists() filter() finallyDo() first() firstOrDefault() flatMap() flatMapIterable() forEach() forIterable() from() fromAction() fromCallable() fromCancellableFuture() fromFunc0() fromFuture() fromRunnable() generate() generateAbsoluteTime() getIterator() groupBy() groupByUntil() groupJoin() ifThen() ignoreElements() interval() isEmpty() join() just() last() lastOrDefault()

Slide 14

Slide 14 text

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

Slide 23

Slide 23 text

SAMPLE ‣ https://jsfiddle.net/hydrakecat/f8tv1phn/

Slide 24

Slide 24 text

ADVICE 3: KNOW THE OPERATOR CATEGORIES ▸ Async ▸ Blocking Observable ▸ Combining ▸ Conditional & Boolean ▸ Connectable Observable ▸ Error Handling ▸ Filtering ▸ Mathematical and Aggregate ▸ Observable Creation ▸ String ▸ Transformational ▸ Utility Operators

Slide 25

Slide 25 text

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#/)