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

Reactive 4TW

Reactive 4TW

Reactive programming is a new big thing for a couple of years now, but it also shrouded with a distrust as any new technology with mindblowing concepts. Lets pretend together that you didn't hear about Reactive and start learning it from scratch!

12d6ff93ca25d366161efccadd81bbb2?s=128

Alexey Buzdin

September 24, 2015
Tweet

More Decks by Alexey Buzdin

Other Decks in Programming

Transcript

  1. Introduction to Reactive principles with RxJava @AlexeyBuzdin Reactive 4TW!

  2. @AlexeyBuzdin Developer / Trainer at

  3. this page was intentionally left blank… Reactive

  4. None
  5. Good?

  6. or Bad?

  7. Scary

  8. but Productive!

  9. reactive /rɪˈaktɪv/ 1. showing a response to a stimulus. "pupils

    are reactive to light" 2. acting in response to a situation rather than creating or controlling it. "a proactive rather than a reactive approach"
  10. reactive /rɪˈaktɪv/ 1. showing a response to a stimulus. "pupils

    are reactive to light" 2. acting in response to a situation rather than creating or controlling it. "a proactive rather than a reactive approach"
  11. None
  12. Observer Observable Object

  13. Observer Observable Object

  14. Observer Observable Object

  15. Observer Observable Object

  16. Observable

  17. Observer

  18. Object(Event)

  19. programming with asynchronous data streams Reactive programming is

  20. List<Ball> balls = Collections.singletonList(new Ball()); Observable<Ball> pitcher = Observable.from(balls); Observer<Ball>

    batter = new Observer<Ball>() { @Override public void onCompleted() { /* Score! */ } @Override public void onError(Throwable e) { /* Shame */ } @Override public void onNext(Ball ball) { /* Hit! */ } }; pitcher.subscribe(batter);
  21. List<Ball> balls = Collections.singletonList(new Ball()); Observable<Ball> pitcher = Observable.from(balls); Observer<Ball>

    batter = new Observer<Ball>() { @Override public void onCompleted() { /* Score! */ } @Override public void onError(Throwable e) { /* Shame */ } @Override public void onNext(Ball ball) { /* Hit! */ } }; pitcher.subscribe(batter);
  22. List<Ball> balls = Collections.singletonList(new Ball()); Observable<Ball> pitcher = Observable.from(balls); Observer<Ball>

    batter = new Observer<Ball>() { @Override public void onCompleted() { /* Score! */ } @Override public void onError(Throwable e) { /* Shame */ } @Override public void onNext(Ball ball) { /* Hit! */ } }; pitcher.subscribe(batter);
  23. List<Ball> balls = Collections.singletonList(new Ball()); Observable<Ball> pitcher = Observable.from(balls); Observer<Ball>

    batter = new Observer<Ball>() { @Override public void onCompleted() { /* Score! */ } @Override public void onError(Throwable e) { /* Shame */ } @Override public void onNext(Ball ball) { /* Hit! */ } }; pitcher.subscribe(batter);
  24. List<Ball> balls = /* Infinite balls */ Observable<Ball> pitcher =

    Observable.from(balls); Observer<Ball> batter = new Observer<Ball>() { @Override public void onCompleted() { /* Score! */ } @Override public void onError(Throwable e) { /* Shame */ } @Override public void onNext(Ball ball) { /* Hit! */ } }; pitcher.subscribe(batter);
  25. programming with asynchronous data streams Reactive programming is

  26. None
  27. Everything can be a Stream: - collections - user inputs

    - hardware - properties - caches - data structures - etc.
  28. Collections List< > balls = Lists.emptyList();

  29. Everything can be a Stream: - collections - user inputs

    - hardware - properties - caches - data structures - etc.
  30. UserInput Button btw = new Button(ctx); btn.setOnClickListener(e -> { //

    Do something })
  31. Everything can be a Stream: - collections - user inputs

    - hardware - properties - caches - data structures - etc.
  32. Hardware LocationManager mng = (LocationManager) getSystemService(Context.LOCATION_SERVICE); LocationListener listener = new

    LocationListener() { public void onLocationChanged(Location location) { // Do something } ….. }; locationManager.requestLocationUpdates (LocationManager.NETWORK_PROVIDER, 0, 0, listener);
  33. Everything can be a Stream: - collections - user inputs

    - hardware - properties - caches - data structures - etc.
  34. UserInput SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); prefs.getBoolean("keystring", true);

  35. Everything can be a Stream: - collections - user inputs

    - hardware - properties - caches - data structures - etc.
  36. DataBase select().from(t).where(row(t.a, t.b).eq(1, 2));

  37. Everything can be a Stream: - collections - user inputs

    - hardware - properties - caches - data structures - etc.
  38. Data Structures

  39. Everything can be a Stream: - collections - user inputs

    - hardware - properties - caches - data structures - etc.
  40. …and what next?

  41. Operations!

  42. Operations! - map - filter - zip - groupBy -

    collect - flatMap - etc.
  43. RealLife RX RxView.clickEvents(tapBtn) .map(new Func1<ViewClickEvent, Integer>() { @Override public Integer

    call(ViewClickEvent onClickEvent) { log("GOT A TAP"); return 1; } }) .buffer(2, TimeUnit.SECONDS) .observeOn(AndroidSchedulers.mainThread()) .subscribe( /* Observer here */)
  44. programming with asynchronous data streams Reactive programming is

  45. None
  46. programming with asynchronous data streams Reactive programming is

  47. RealLife RX

  48. None
  49. None
  50. Try it Out!

  51. Q&A