Upgrade to Pro — share decks privately, control downloads, hide ads and more …

The Beginner's Guide to RxJava

Hiroshi Kurokawa
September 16, 2016

The Beginner's Guide to RxJava

The Beginner's Guide to RxJava by Hiroshi Kurokawa
Published July 21, 2016 in Technology

In this talk, I'm giving some advice about how to learn RxJava.

Reference
- RxJava - Wiki
https://github.com/ReactiveX/RxJava/wiki
- Introduction to Rx
http://www.introtorx.com/
- 101 Rx Samples
http://rxwiki.wikidot.com/101samples
- RxJS
http://reactivex.io/rxjs/
- JSFiddle
https://jsfiddle.net
- RxJS Sample - hydrakecat
https://jsfiddle.net/hydrakecat/f8tv1phn/
- RxJava Operators
http://reactivex.io/documentation/operators.html

Hiroshi Kurokawa

September 16, 2016
Tweet

More Decks by Hiroshi Kurokawa

Other Decks in Technology

Transcript

  1. WHAT’S RXJAVA ▸ Handle a sequence of data over time

    as a stream ▸ Instead of gathering them, responding to each of them
  2. 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
  3. 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. 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
  5. 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);
  6. WHY DIFFICULT? 1. Many operators all() amb() ambWith() and() asyncAction()

    asyncFunc() averageDouble() averageFloat() averageInteger() averageLong() buffer() cache() cast() chunkify() collect() combineLatest() concat() concatMap() concatWith()
  7. 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()
  8. 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()
  9. 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()
  10. WHY DIFFICULT? 1. Many operators 2. Some complicated concepts ‣

    Hot/Cold observable ‣ Scheduler ‣ Subscription
  11. WHY DIFFICULT? 1. Many operators 2. Some complicated concepts ‣

    Hot/Cold observable ‣ Scheduler ‣ Subscription ‣ Subject
  12. WHY DIFFICULT? 1. Many operators 2. Some complicated concepts ‣

    Hot/Cold observable ‣ Scheduler ‣ Subscription ‣ Subject ‣ Back pressure
  13. 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
  14. GUIDE 2: GET USED TO OPERATORS ▸ Play with Rx

    operators ▸ Understand how operators work with your heart
  15. 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
  16. GUIDE 3: KNOW THE OPERATOR CATEGORIES ▸ Creating Observables ▸

    Transforming Observables ▸ Filtering Observables ▸ Combining Observables ▸ Error Handling Observables ▸ Observable Utility Operators ▸ Conditional and Boolean Operators ▸ Mathematical and Aggregate Operators ▸ Connectable Observable Operators
  17. CREATING OBSERVABLES ▸ just() ▸ empty() / never() / error()

    ▸ from() ▸ interval() / timer() ▸ range()
  18. SOME HINTS ‣ Are you interested in more than one

    Observables?
 → Combining / Conditional
  19. SOME HINTS ‣ Are you interested in more than one

    Observables?
 → Combining / Conditional ‣ Or just one Observable?
 → Transforming / Filtering
  20. SOME HINTS ‣ Are you interested in more than one

    Observables?
 → Combining / Conditional ‣ Or just one Observable?
 → Transforming / Filtering ‣ Are you creating an Observable?
 → Creating
  21. 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)
  22. EXAMPLE 1: NETWORK CALL ‣ Wait until both the network

    calls return and then combine the responses
  23. EXAMPLE 1: NETWORK CALL ‣ Wait until both the network

    calls return and then combine the responses ‣ Use zipWith()
  24. 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);
  25. 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);
  26. EXAMPLE 2: HANDLE UI EVENTS ‣ Enable a button only

    when all the input fields are valid
  27. EXAMPLE 2: HANDLE UI EVENTS ‣ Enable a button only

    when all the input fields are valid ‣ Use combineLatest()
  28. 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);
  29. EXAMPLE 2: HANDLE UI EVENTS ‣ Periodically do something while

    list view is idling ‣ Use takeUntil()
  30. EXAMPLE 2: HANDLE UI EVENTS ‣ Periodically do something while

    list view is idling ‣ Use takeUntil() ConnectableObservable<Boolean> 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(".")));
  31. GUIDE 4: TAKE ADVANTAGE OF EXISTING RX LIBRARIES ‣ Don’t

    create your Observable with Observable.create()
  32. GUIDE 4: TAKE ADVANTAGE OF EXISTING RX LIBRARIES ‣ Don’t

    create your Observable with Observable.create() ‣ Use libraries instead
  33. 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
  34. 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.
  35. 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