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

Rxify - A talk to Remember - CODELAB

ragdroid
January 24, 2017

Rxify - A talk to Remember - CODELAB

ragdroid

January 24, 2017
Tweet

More Decks by ragdroid

Other Decks in Technology

Transcript

  1. `Rxify` Presented By : Garima Jain @ragdroid A Talk to

    Remember A Walk to Remember A Walk-through Remember
  2. `Rxify` Presented By : Garima Jain @ragdroid A Talk to

    Remember A Walk to Remember A Walk-through Remember - CODELAB -
  3. `Rxify` is for? • Already working on RxJava - More

    to learn. • Beginner - Time to start Rxifying your apps
  4. `Rxify` is for? • Already working on RxJava - More

    to learn. • Beginner - Time to start Rxifying your apps • Pros - Sit and relax!
  5. • `Rxify` - term signifies use of RxJava • RxJava

    makes our lives simpler. • Cast a spell (use an operator) and we are good to go. `Rxify`
  6. • Reactive Extensions for JVM (Java Virtual Machine). • Observer

    pattern for sequences of data / events. RxJava
  7. RxJava makes lives simpler • Never having to worry about

    : • Low-level Threading • Synchronisation • Background Processing
  8. RxJava operators at first glance • Complex • Hard to

    understand • Marble Diagrams? What!!
  9. • Observable produces items, • Subscriber consumes those items. •

    Observable calls : (Actions) • onNext() multiple times • followed by onComplete() • OR onError() Basics
  10. • Operators : • manipulate items between producer and consumer.

    • Schedulers • Main Thread • Computation (Background Thread) • Test Scheduler Basics
  11. TakeUntil “discard any items emitted by an Observable after a

    second Observable emits an item or terminates"
  12. Reduc-to “apply a function to each item emitted by an

    Observable, sequentially, and emit the final value”
  13. Input : Observable.fromIterable(Arrays.asList( "Color : Red", "Size : M", "Occasion

    : Casual", "Type : T-Shirt")); //TODO Concatenate filters to send to API
 Desired Output : "Color : Red | Size : M | Occasion : Casual | Type : T-Shirt"
 Reduc-to
  14. Reduc-to Solution : inputValues
 .reduce(new BiFunction<String, String, String>() {
 @Override


    public String apply(String old, String value) throws Exception {
 return old + " | " + value;
 }
 })
 .defaultIfEmpty("No item")
 .toObservable()
  15. Map “Transform the items emitted by an Observable by applying

    a function to each item” - source Transforms every element of a collection of items.
  16. Map-io Solution : Observable.intervalRange(1, 10, 100, 500, TimeUnit.MILLISECONDS) .map(new Function<Long,

    Long>() {
 @Override
 public Long apply(Long aLong) throws Exception {
 return aLong * 5;
 }
 })
  17. FlatMap Transforms every element of a collection into another collection

    of items and combines them into a single collection.
  18. FlatMap “Transform the items emitted by an Observable into Observables,

    then flatten the emissions from those into a single Observable” - source Transforms every element of a collection into another collection of items and combines them into a single collection.
  19. Solution : Observable.fromIterable(Arrays.asList("Hello World!", "How Are You?")) .flatMap(new Function<String, ObservableSource<String>>()

    {
 @Override
 public ObservableSource<String> apply(String inputString) throws Exception {
 return Observable.fromArray(inputString.split(" "));
 }
 }) Flatmap-um
  20. Concat “Emit the emissions from two or more Observables without

    interleaving them” source “Combine multiple Observables into one by merging their emissions” source Merge
  21. Concat Merge 1 2 3 4 8 9 1 2

    3 4 8 9 1 2 3 4 8 9 1 2 3 4 8 9 Concat-ify Merge-os
  22. Fictional Problem (Snape’s Assignment) • Professor Snape has requested all

    students to write an essay on werewolves. • The students who will turn in the essay first will get more points than the ones submitting later. • Students are divided into four house observables : • GryffindorObservable (G), • SlytherinObservable (S), • HufflepuffObservable (H) and • RavenclawObservable(R)).
  23. Observable <Gryffindor> R1 R2 H1 H2 Observable <Ravenclaw> Observable <Hufflepuff>

    G1 G2 S1 S2 Observable <Slytherin> Snape’s Assignment - Submission
  24. Snape’s Assignment - Draco’s trick S1 S2 H1 H2 R1

    R2 G1 G2 Concat-ify Observable <Gryffindor> R1 R2 H1 H2 Observable <Ravenclaw> Observable <Hufflepuff> G1 G2 S1 S2 Observable <Slytherin>
  25. Check it Out! via-Hedwig Checkout commit : “Start of Part

    3, Assignment Evaluator" git checkout <sha>
  26. Snape’s Assignment - Hermione’s fix G1 S1 G2 R1 H1

    S2 R2 H2 Merge-os Observable <Gryffindor> R1 R2 H1 H2 Observable <Ravenclaw> Observable <Hufflepuff> G1 G2 S1 S2 Observable <Slytherin>
  27. Fictional Problem (The Battle) • Dumbledore’s army (Harry, Hermione, Ron

    and others) • Death Eaters (Lucius Malfoy, Bellatrix Lestrange and others) • Each team is casting lethal spells against the other team. • Dumbledore’s army is not as experienced as the Death Eaters. • Spells produced by Death Eaters is overwhelming the Dumbledore’s army (consumer).
  28. Buffer Buffer small chunks of items and emit them instead

    of emitting one item at a time. “Periodically gather items emitted by an Observable into bundles and emit these bundles rather than emitting the items one at a time" - source
  29. Observable <Spell> Buffer Observable <Spell> 1 2 3 4 1

    2 getSpellsObservable()
 .buffer(2); //count 3 4
  30. The Battle Solution : Observable.interval(1, 1, TimeUnit.MICROSECONDS) .take(129)
 .buffer(10)
 .flatMap(new

    Function<List<Long>, ObservableSource<String>>() {
 @Override
 public ObservableSource<String> apply(List<Long> longs) throws Exception {
 return Observable.fromIterable(longs)
 .reduce("", new BiFunction<String, Long, String>() {
 @Override
 public String apply(String oldString, Long aLong) throws Exception {
 return oldString + "\n" + aLong;
 }
 }).toObservable();
 }
 })
  31. Debounce From a list of items emit an item only

    when, some time has passed since it last emitted anything.
  32. Debounce From a list of items emit an item only

    when, some time has passed since it last emitted anything. “Only emit an item from an Observable if a particular timespan has passed without it emitting another item" - source
  33. Observable <Spell> 1 2 3 4 Observable <Spell> 1 2

    4 getSpellsObservable()
 .debounce(300, TimeUnit.MILLISECONDS); Debounce
  34. Backpressure Frequency of Producer producing an item is more than

    the ability of Consumer to consume. “Strategies for coping with Observables that produce items more rapidly than their observers consume them” - source
  35. The Battle Input : Flowable.interval(1, 1, TimeUnit.MICROSECONDS); //TODO Go inside

    FlowableInterval class to see where the Exception is thrown

  36. The Battle Input : Flowable.interval(1, 1, TimeUnit.MICROSECONDS); • Flowable.interval() method

    is tagged as @BackpressureSupport(BackpressureKind.ERROR) • ERROR : The operator will emit a MissingBackpressureException if the downstream didn't request enough or in time. • Consumers should consider applying one of the {@code onBackpressureXXX} operators as well.
  37. testGetFluxWeed() - TestObserver //TODO Create a TestObserver
 
 //TODO subscribe

    to getFluxWeed()
 
 //TODO tell observer to wait for terminal event
 
 //TODO assert there are no errors
 
 //TODO assert we received onComplete
  38. Solution : //TODO Create a TestObserver
 TestObserver<Object> testObserver = TestObserver.create();


    //TODO subscribe to getFluxWeed()
 magicalDataSource.getFluxWeed().subscribe(testObserver);
 //TODO tell observer to wait for terminal event
 testObserver.awaitTerminalEvent();
 //TODO assert there are no errors
 testObserver.assertNoErrors();
 //TODO assert we received onComplete
 testObserver.assertComplete(); testGetFluxWeed() - TestObserver
  39. testGetFluxWeedFlowable() - TestObserver //TODO Create a TestSubscriber
 
 //TODO subscribe

    to getFluxWeed()
 
 //TODO tell subscriber to wait for terminal event
 
 //TODO assert there are no errors
 
 //TODO assert we received onComplete
  40. Solution : 
 //TODO Create a TestSubscriber
 TestSubscriber<Object> testSubscriber =

    TestSubscriber.create();
 //TODO subscribe to getFluxWeed()
 magicalDataSource.getFluxWeedFlowable().subscribe(testSubscriber);
 //TODO tell subscriber to wait for terminal event
 testSubscriber.awaitTerminalEvent();
 //TODO assert there are no errors
 testSubscriber.assertNoErrors();
 //TODO assert we received onComplete
 testSubscriber.assertComplete(); testGetFluxWeedFlowable() - TestObserver
  41. • Learn by practice. • Make RxJava your friend and

    not a foe. • Learn by examples - Kaushik Gopal RxJava. https://github.com/kaushikgopal/RxJava-Android-Samples • Codelab - Full Extended Text version (coming soon on) : https://github.com/ragdroid/rxify/tree/codelab • My Medium blogs for more details. https://medium.com/@ragdroid What’s Next