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

The Beginner's Guide to RxJava

The Beginner's Guide to RxJava

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

Reference
- RxJava - Wiki
https://github.com/ReactiveX/RxJava/wiki
- 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

July 21, 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. ADVISE 2: GET USED TO OPERATORS ▸ Play with Rx

    operators ▸ Understand how operators work with your heart
  14. 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
  15. 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
  16. SOME HINTS ‣ Are you interested in more than one

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

    Observables?
 → Combining / Conditional ‣ Or just one Observable?
 → Transforming / Filtering
  18. 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
  19. 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)
  20. EXAMPLE 1: NETWORK CALL ‣ Wait until both the network

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

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

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

    when all the input fields are valid ‣ Use combineLatest()
  26. 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);
  27. 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
  28. 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
  29. 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);
  30. ADVICE 4: TAKE ADVANTAGE OF EXISTING RX LIBRARIES ‣ Don’t

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

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