Ce diaporama a bien été signalé.
Nous utilisons votre profil LinkedIn et vos données d’activité pour vous proposer des publicités personnalisées et pertinentes. Vous pouvez changer vos préférences de publicités à tout moment.

Building a reactive mindset

367 vues

Publié le

So you have heard about RxJava, maybe checked out some articles, videos or even code - but things are still not that clear!? The aim of this talk is first of all to create the functional reactive programming mindset; to explain what an Observable and a Subscriber are; and to give a bit of insight in the magic of thread handling in RxJava.
We will then take a practical example, see how RxJava can be applied and what its advantages are compared to other options.
I will end the talk by addressing an important question: how to best proceed to integrate RxJava into an existing app.

Publié dans : Logiciels
  • Soyez le premier à commenter

Building a reactive mindset

  1. 1. @FMuntenescu Florina Muntenescu @FMuntenescu Building a Reactive Mindset
  2. 2. @FMuntenescu Florina Muntenescu @FMuntenescu Building a Reactive Mindset
  3. 3. @FMuntenescu Donald Duck’s Burger Joint
  4. 4. @FMuntenescu Huey Dewey Louie
  5. 5. @FMuntenescu Huey
  6. 6. @FMuntenescu Dewey
  7. 7. @FMuntenescu Louie
  8. 8. @FMuntenescu Huey Dewey Louie
  9. 9. @FMuntenescu filter(meat -> isMeatFresh(meat)) map(meat -> cook(meat)) Dewey
  10. 10. @FMuntenescu zip
  11. 11. @FMuntenescu zip
  12. 12. @FMuntenescu zip
  13. 13. @FMuntenescu zip
  14. 14. @FMuntenescu STREAMS OF DATA
  15. 15. @FMuntenescu STREAMS OF DATA
  16. 16. @FMuntenescu STREAMS OF DATA
  17. 17. @FMuntenescu zip STREAMS OF DATA
  18. 18. @FMuntenescu Consumer Observable Subscriber
  19. 19. @FMuntenescu filter(meat -> isMeatFresh(meat)) map(meat -> cook(meat)) operators
  20. 20. @FMuntenescu Huey.getBun()
  21. 21. @FMuntenescu filter(meat -> isMeatFresh(meat)) for(Meat meat: meatList){ if(!isMeatFresh(meat)){ meatList.remove(meat); } }
  22. 22. @FMuntenescu Observable.from(meatSource) .filter(meat -> isMeatFresh(meat)) filter(meat -> isMeatFresh(meat))
  23. 23. @FMuntenescu filter(meat -> isMeatFresh(meat)) map(meat -> cook(meat)) for(Meat meat: meatList){ meat = cook(meat); }
  24. 24. @FMuntenescu Observable.from(meatSource) .filter(meat -> isMeatGoneBad(meat)) .map(meat -> cook(meat)); filter(meat -> isMeatFresh(meat)) map(meat -> cook(meat))
  25. 25. @FMuntenescu Observable<Burger> burgerObservable = Observable.zip(bunObservable, meatObservable, tomatoObservable, (bun, meat, tomato) -> makeBurger(bun, meat, tomato)) bunObservable meatObservable tomatoObservable burgerObservable
  26. 26. @FMuntenescu Observable<Burger> burgerObservable = Observable.zip(bunObservable, meatObservable, tomatoObservable, (bun, meat, tomato) -> makeBurger(bun, meat, tomato)) bunObservable meatObservable tomatoObservable burgerObservable
  27. 27. @FMuntenescu Consumer Observable Subscriber .subscribe
  28. 28. @FMuntenescu Subscriber burgerObservable .subscribe(mickeySubscriber);
  29. 29. @FMuntenescu Subscriber onNext(Burger burger) onCompleted() burgerObservable .subscribe( burger -> eatBurger(burger), error -> complain(error), () -> leaveBurgerJoint()); // onNext //onError //onCompleted onError(Throwable error)
  30. 30. @FMuntenescu Observable.zip(bunObservable, meatObservable, tomatoObservable, (bun, meat, tomato) -> makeBurger(bun, meat, tomato)) .subscribe(burger -> eatBurger(burger), error -> complain(error)); // dining area // dining area
  31. 31. @FMuntenescu // kitchen Observable.zip(bunObservable, meatObservable, tomatoObservable, (bun, meat, tomato) -> makeBurger(bun, meat, tomato)) .subscribe(burger -> eatBurger(burger), error -> complain(error)); // dining area
  32. 32. @FMuntenescu // kitchen // dining area Observable.zip(bunObservable, meatObservable, tomatoObservable, (bun, meat, tomato) -> makeBurger(bun, meat, tomato)) .subscribeOn(kitchen) .observeOn(dining area) .subscribe(burger -> eatBurger(burger), error -> complain(error));
  33. 33. @FMuntenescu Observable.zip(bunObservable, meatObservable, tomatoObservable, (bun, meat, tomato) -> makeBurger(bun, meat, tomato)) .subscribeOn(Schedulers.computation()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(burger -> eatBurger(burger), error -> complain(error)); // kitchen // dining area
  34. 34. @FMuntenescu Stream of data Observable Subscriber Operators Threads
  35. 35. @FMuntenescu Creating Observables
  36. 36. @FMuntenescu Creating Observables From a List
  37. 37. @FMuntenescu List<Burger> burgers = new ArrayList<>(); burgers.add(new Burger()); burgers.add(new Burger()); Observable<Burger> burgerObservable = Observable.from(burgers);
  38. 38. @FMuntenescu List<Burger> burgers = new ArrayList<>(); burgers.add(new Burger()); burgers.add(new Burger()); Observable<Burger> burgerObservable = Observable.from(burgers); 1 2
  39. 39. @FMuntenescu Observable<Burger> burgerObservable = Observable.from(burgers); burgerObservable.subscribe(new Subscriber<Burger>() { @Override public void onNext(final Burger burger) { } @Override public void onCompleted() { } @Override public void onError(final Throwable e) { } });
  40. 40. @FMuntenescu Observable<Burger> burgerObservable = Observable.from(burgers); burgerObservable.subscribe(new Subscriber<Burger>() { @Override public void onNext(final Burger burger) { // handle burger 1 } @Override public void onCompleted() { } @Override public void onError(final Throwable e) { } }); 1
  41. 41. @FMuntenescu Observable<Burger> burgerObservable = Observable.from(burgers); burgerObservable.subscribe(new Subscriber<Burger>() { @Override public void onNext(final Burger burger) { // handle burger 2 } @Override public void onCompleted() { } @Override public void onError(final Throwable e) { } }); 1 2
  42. 42. @FMuntenescu Observable<Burger> burgerObservable = Observable.from(burgers); burgerObservable.subscribe(new Subscriber<Burger>() { @Override public void onNext(final Burger burger) { } @Override public void onCompleted() { // no more burgers! } @Override public void onError(final Throwable e) { } }); 1 2
  43. 43. @FMuntenescu Observable<List<Burger>> List<Burger> burgers = new ArrayList<>(); burgers.add(new Burger()); burgers.add(new Burger()); burgerObservable = Observable.just(burgers);
  44. 44. @FMuntenescu 1 Observable<List<Burger>> burgerObservable = Observable.just(burgers); List<Burger> burgers = new ArrayList<>(); burgers.add(new Burger()); burgers.add(new Burger());
  45. 45. @FMuntenescu Observable<List<Burger>> burgerObservable = Observable.just(burgers); burgerObservable.subscribe( new Subscriber<List<Burger>>() { @Override public void onNext(final List<Burger> burgers) { } @Override public void onCompleted() { } @Override public void onError(final Throwable e) { } });
  46. 46. @FMuntenescu Observable<List<Burger>> burgerObservable = Observable.just(burgers); burgerObservable.subscribe( new Subscriber<List<Burger>>() { @Override public void onNext( final List<Burger> burgers) { // handle the list of burgers } @Override public void onCompleted() { } @Override public void onError(final Throwable e) { } }); 1
  47. 47. @FMuntenescu Observable<List<Burger>> burgerObservable = Observable.just(burgers); burgerObservable.subscribe( new Subscriber<List<Burger>>() { @Override public void onNext( final List<Burger> burgers) { // handle the list of burgers } @Override public void onCompleted() { // no more burgers! } @Override public void onError(final Throwable e) { } }); 1
  48. 48. @FMuntenescu Creating Observables From Click Events
  49. 49. @FMuntenescu
  50. 50. @FMuntenescu
  51. 51. @FMuntenescu
  52. 52. @FMuntenescu [click!] [click!] [click!]
  53. 53. @FMuntenescu ???.subscribe(new Subscriber<String>() { @Override public void onNext(final String s) { // called on every click } @Override public void onCompleted() { } @Override public void onError(final Throwable e) { } });
  54. 54. @FMuntenescu [click!] [click!] [click!] ???.onNext(…) ???.onNext(…) ???.onNext(…)
  55. 55. @FMuntenescu burgerButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(final View view) { } }); PublishSubject<String> burgerRequestedSubject = PublishSubject.create();
  56. 56. @FMuntenescu burgerButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(final View view) { burgerRequestedSubject.onNext(…); } }); PublishSubject<String> burgerRequestedSubject = PublishSubject.create();
  57. 57. @FMuntenescu burgerRequestedSubject.subscribe(new Subscriber<String>() { @Override public void onNext(final String s) { // called on every click } @Override public void onCompleted() { // will never be called for click events } @Override public void onError(final Throwable e) { } });
  58. 58. @FMuntenescu Creating Observables From Declarativ e Code
  59. 59. @FMuntenescu public Burger cook(){ // cooking is time consuming return burger; } Observable<Burger> burgerObservable = Observable.fromCallable(this::cook);
  60. 60. @FMuntenescu Error Handling
  61. 61. @FMuntenescu Thread t = new Thread(new Runnable() { @Override public void run() { cook(); } });
  62. 62. @FMuntenescu Thread t = new Thread(new Runnable() { @Override public void run() { try{ cook(); } catch(Exception e) { // handle exception } } });
  63. 63. @FMuntenescu getMeatObservable() .filter(meat -> isMeatFresh(meat)) .map(meat -> cook(meat)) .subscribeOn( meat -> doSomething(), error -> complain(error));
  64. 64. @FMuntenescu getMeatObservable() .filter(meat -> isMeatFresh(meat)) .map(meat -> cook(meat)) .subscribeOn( meat -> doSomething(), error -> complain(error));
  65. 65. @FMuntenescu public Observable<Meat> getCookedMeat() { return getMeatObservable() .filter(meat -> isMeatFresh(meat)) .map(meat -> cook(meat)); } Observable.zip(getBun(), getCookedMeat(), getTomatoSlice(), (bun, meat, tomato) -> makeBurger(bun, meat, tomato)) .subscribeOn( burger -> eatBurger(burger), error -> complain(error));
  66. 66. @FMuntenescu public Observable<Meat> getCookedMeat() { return getMeatObservable() .filter(meat -> isMeatFresh(meat)) .map(meat -> cook(meat)); } Observable.zip(getBun(), getCookedMeat(), getTomatoSlice(), (bun, meat, tomato) -> makeBurger(bun, meat, tomato)) .subscribeOn( burger -> eatBurger(burger), error -> complain(error));
  67. 67. @FMuntenescu Start using RxJava
  68. 68. @FMuntenescu public Burger cook(){ // cooking is time consuming // make sure it’s on a background thread return burger; } Observable<Burger> burgerObservable = Observable.fromCallable(this::cook) .subscribeOn(Schedulers.computation()) .observeOn(AndroidSchedulers.mainThread())
  69. 69. @FMuntenescu @GET(“repos”) Call<List<Burger>> repoBurgers(); @GET(“repos”) Observable<List<Burger>> repoBurgers();
  70. 70. @FMuntenescu
  71. 71. @FMuntenescu@FMuntenescu jobs@updudes.net Building a Reactive Mindset https://upday.github.io/blog/reactive_mindset_burgers Reactive Burgers – Code Example https://github.com/florina-muntenescu/ReactiveBurgers Building a Burger Reactive Mindset

×