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

Crunching RxAndroid

Crunching RxAndroid

Let's dive into Reactive Programming on Android

Roberto Orgiu

March 07, 2016
Tweet

More Decks by Roberto Orgiu

Other Decks in Programming

Transcript

  1. CRUNCHING RXANDROID ABOUT THE SPEAKER Android 2.1+ Android Developer @

    Ennova twitter.com/_tiwiz github.com/tiwiz medium.com/@_tiwiz ROBERTO ORGIU
  2. CRUNCHING RXANDROID A FEW STEPS BACK ▸ DESIGN PATTERNS ▸

    OBSERVER PATTERN ▸ IMMUTABILITY ▸ FUNCTIONAL PROGRAMMING ▸ REACTIVE EXTENSIONS
  3. A DESIGN PATTERN IS A GENERAL REPEATABLE SOLUTION TO A

    COMMONLY OCCURRING PROBLEM Wikipedia and a bunch of other people CRUNCHING RXANDROID DESIGN PATTERNS
  4. DESIGN PATTERNS Erich Gamma, Richard Helm, Ralph Johnson, John Glissades

    ELEMENTS OF REUSABLE OBJECTED-ORIENTED SOFTWARE
  5. CRUNCHING RXANDROID A FEW STEPS BACK ▸ DESIGN PATTERNS ▸

    OBSERVER PATTERN ▸ IMMUTABILITY ▸ FUNCTIONAL PROGRAMMING ▸ REACTIVE EXTENSIONS
  6. AN OBJECT MAINTAINS A LIST OF ITS OBSERVERS AND NOTIFIES

    THEM AUTOMATICALLY OF ANY STATE CHANGES Wikipedia CRUNCHING RXANDROID OBSERVER PATTERN
  7. CRUNCHING RXANDROID A FEW STEPS BACK ▸ DESIGN PATTERNS ▸

    OBSERVER PATTERN ▸ IMMUTABILITY ▸ FUNCTIONAL PROGRAMMING ▸ REACTIVE EXTENSIONS
  8. AN IMMUTABLE OBJECT IS AN OBJECT WHOSE STATE CANNOT BE

    MODIFIED AFTER IT IS CREATED. BRIAN GOETZ, ORACLE CRUNCHING RXANDROID IMMUTABILITY
  9. CRUNCHING RXANDROID A FEW STEPS BACK ▸ DESIGN PATTERNS ▸

    OBSERVER PATTERN ▸ IMMUTABILITY ▸ FUNCTIONAL PROGRAMMING ▸ REACTIVE EXTENSIONS
  10. TREATS COMPUTATION AS THE EVALUATION OF MATHEMATICAL FUNCTIONS AND AVOIDS

    CHANGING STATE AND MUTABLE DATA. Wikipedia CRUNCHING RXANDROID FUNCTIONAL PROGRAMMING
  11. CRUNCHING RXANDROID REACTIVE EXTENSIONS ▸ PUSH (Observable) VS PULL (Iterable)

    ▸ .NET Came in first ▸ Netflix and the JVM ▸ Android bindings
  12. CRUNCHING RXANDROID OBSERVABLES Observable.create(new Observable.OnSubscribe<String>() {
 @Override
 public void call(Subscriber<?

    super String> subscriber) {
 subscriber.onNext("Hello, world!");
 subscriber.onCompleted();
 }
 }); onNext() onCompleted() onError()
  13. CRUNCHING RXANDROID OBSERVABLES Observable.create(new Observable.OnSubscribe<String>() {
 @Override
 public void call(Subscriber<?

    super String> subscriber) {
 subscriber.onNext("Hello, world!");
 subscriber.onCompleted();
 }
 }); Observable.create()
  14. CRUNCHING RXANDROID SUBSCRIBERS observable.subscribe(new Subscriber<String>() {
 @Override
 public void onCompleted()

    {}
 
 @Override
 public void onError(Throwable e) {}
 
 @Override
 public void onNext(String s) {}
 });
  15. OPERATORS CAN BE CHAINED, AND EACH ONE OF THEM WILL

    OPERATE SEQUENTIALLY ON THE OBSERVABLE GENERATED BY THE OPERATOR IMMEDIATELY PREVIOUS IN THE CHAIN CRUNCHING RXANDROID OPERATORS ReactiveX.io
  16. CRUNCHING RXANDROID OPERATORS ▸ Create ▸ Transform ▸ Filter ▸

    Combine ▸ Error handling ▸ Utility ▸ Backpressure
  17. CRUNCHING RXANDROID OPERATORS ▸ create() ▸ just() ▸ from() ▸

    defer() ▸ interval() ▸ range() ▸ timer() Create
  18. CRUNCHING RXANDROID OPERATORS ▸ Create ▸ Transform ▸ Filter ▸

    Combine ▸ Error handling ▸ Utility ▸ Backpressure
  19. CRUNCHING RXANDROID OPERATORS ▸ flatMap()
 
 
 ▸ map() ▸

    groupBy() Transform transform the items emitted by an Observable into Observables, then flatten the emissions from those into a single Observable transform the items emitted by an Observable by applying a function to each item divide an Observable into a set of Observables that each emit a different group of items from the original Observable, organised by key
  20. CRUNCHING RXANDROID OPERATORS ▸ Create ▸ Transform ▸ Filter ▸

    Combine ▸ Error handling ▸ Utility ▸ Backpressure
  21. CRUNCHING RXANDROID OPERATORS ▸ filter() ▸ first() ▸ take() ▸

    debounce() Filter emit only those items from an Observable that pass a predicate test emit only the first item (or the first item that meets some condition) emitted by an Observable emit only the first n items emitted by an Observable only emit an item from an Observable if a particular timespan has passed without it emitting another item
  22. CRUNCHING RXANDROID OPERATORS ▸ Create ▸ Transform ▸ Filter ▸

    Combine ▸ Error handling ▸ Utility ▸ Backpressure
  23. CRUNCHING RXANDROID OPERATORS ▸ combineLatest() ▸ switch() ▸ retry() Combine

    & Error handling when an item is emitted by either of two Observables, combine the latest item emitted by each Observable via a specified function and emit items based on the results of this function convert an Observable that emits Observables into a single Observable that emits the items emitted by the most-recently-emitted of those Observables if a source Observable sends an onError notification, resubscribe to it in the hopes that it will complete without error
  24. CRUNCHING RXANDROID OPERATORS ▸ Create ▸ Transform ▸ Filter ▸

    Combine ▸ Error handling ▸ Utility ▸ Backpressure
  25. TO INTRODUCE MULTITHREADING INTO YOUR CASCADE OF OBSERVABLE OPERATORS, INSTRUCT

    THOSE OPERATORS TO OPERATE ON PARTICULAR SCHEDULERS. ReactiveX.io CRUNCHING RXANDROID SCHEDULERS
  26. CRUNCHING RXANDROID SCHEDULERS ▸ Schedulers.computation() ▸ Schedulers.from(executor) ▸ Schedulers.immediate() ▸

    Schedulers.io() ▸ Schedulers.newThread() ▸ Schedulers.trampoline() ▸ AndroidSchedulers.mainThread()
  27. CRUNCHING RXANDROID SCHEDULERS ▸ subscribeOn(scheduler)
 ▸ observeOn(scheduler) How to use

    them On what Scheduler shall the Observable run? and on which shall it return its result?
  28. ACT AS A BRIDGE BETWEEN OBSERVABLES AND OBSERVERS, AS THEY

    ARE BOTH. ReactiveX.io CRUNCHING RXANDROID SUBJECTS
  29. CRUNCHING RXANDROID SUBJECTS AsyncSubject it emits the last value (and

    only the last value) emitted by the source Observable, and only after that source Observable completes. (If the source Observable does not emit any values, the AsyncSubject also completes without emitting any values.)
  30. CRUNCHING RXANDROID SUBJECTS BehaviorSubject When an observer subscribes to this

    type of Subject, it begins by emitting the item most recently emitted by the source Observable (or a seed/default value if none has yet been emitted) and then continues to emit any other items emitted later by the source Observable(s).
  31. CRUNCHING RXANDROID SUBJECTS PublishSubject it emits to an observer only

    those items that are emitted by the source Observable(s) subsequent to the time of the subscription.
  32. CRUNCHING RXANDROID SUBJECTS ReplaySubject it emits to any observer all

    of the items that were emitted by the source Observable(s), regardless of when the observer subscribes.
  33. SUBJECTS CAN BE HANDY WHEN MANAGING NETWORKING REQUESTS AND ANDROID

    LIFECYCLE CHANGES. CRUNCHING RXANDROID SUBJECTS
  34. CRUNCHING RXANDROID USEFUL STUFF ▸ RxAndroid
 ▸ RxLifecycle
 ▸ RxBinding


    ▸ Retrofit Libraries from Trello from Jake Wharton from Square from ReactiveX
  35. CRUNCHING RXANDROID USEFUL STUFF ▸ Dan Lew Codes
 ▸ Advanced

    RxJava
 ▸ CoseNonJaviste 
 Blogs akarnokd.blogspot.it cosenonjaviste.it/rxjava blog.danlew.net