Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

An Introduction to RxJava

59 views

Published on

An Introduction to RxJava

Published in: Technology
  • Be the first to comment

  • Be the first to like this

An Introduction to RxJava

  1. 1. An Introduction to RxJava Matt Dupree
  2. 2. mSearchView.setOnQueryTextListener(
 new SearchView.OnQueryTextListener() {
 @Override
 public boolean onQueryTextSubmit(String s) {
 mSearchView.clearFocus();
 return true;
 }
 
 @Override
 public boolean onQueryTextChange(String s) {
 searchFor(s);
 return true;
 }
 });
  3. 3. mSearchView.setOnQueryTextListener(
 new SearchView.OnQueryTextListener() {
 @Override
 public boolean onQueryTextSubmit(String s) {
 mSearchView.clearFocus();
 return true;
 }
 
 @Override
 public boolean onQueryTextChange(String s) {
 searchFor(s);
 return true;
 }
 });
  4. 4. private void searchFor(String query) {
 // ANALYTICS EVENT: Start a search on the Search activity
 // Contains: Nothing (Event params are constant: Search query 
 // not included)
 AnalyticsHelper.sendEvent(SCREEN_LABEL, "Search", "");
 Bundle args = new Bundle(1);
 if (query == null) {
 query = "";
 }
 args.putString(ARG_QUERY, query);
 if (TextUtils.equals(query, mQuery)) {
 getLoaderManager()
 .initLoader(SearchTopicsSessionsQuery.TOKEN, args,
 this);
 } else {
 getLoaderManager()
 .restartLoader(SearchTopicsSessionsQuery.TOKEN, args,
 this);
 }
 mQuery = query;
 }
  5. 5. private void searchFor(String query) {
 // ANALYTICS EVENT: Start a search on the Search activity
 // Contains: Nothing (Event params are constant: Search query 
 // not included)
 AnalyticsHelper.sendEvent(SCREEN_LABEL, "Search", "");
 Bundle args = new Bundle(1);
 if (query == null) {
 query = "";
 }
 args.putString(ARG_QUERY, query);
 if (TextUtils.equals(query, mQuery)) {
 getLoaderManager()
 .initLoader(SearchTopicsSessionsQuery.TOKEN, args,
 this);
 } else {
 getLoaderManager()
 .restartLoader(SearchTopicsSessionsQuery.TOKEN, args,
 this);
 }
 mQuery = query;
 }
  6. 6. @Override
 public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
 mResultsAdapter.swapCursor(data);
 mSearchResults.setVisibility(
 data.getCount() > 0 ? View.VISIBLE : View.GONE);
 }
  7. 7. we’re missing an abstraction
  8. 8. Observable
  9. 9. declaratively composable sequence
  10. 10. declaratively composable sequence
  11. 11. declaratively composable sequence
  12. 12. Observables in Action
  13. 13. I was pretty much dragged into RxJava by my coworkers...[RxJava] was a lot like git...when I first learned git, I didn’t really learn it. I just spent three weeks being mad at it...and then something clicked and I was like ‘Oh! I get it! And this is amazing and I love it!' The same thing happened with RxJava. —Dan Lew, Google Developer Expert
  14. 14. Java’s Array Memory Composition Rx Observable
  15. 15. Java’s Array Memory Composition Rx Observable
  16. 16. Java’s Array Memory Composition Rx Observable
  17. 17. Java’s Array Java’s Iterable Memory Composition Kotlin’s Iterable; Java 8 Streams Rx Observable
  18. 18. Java’s Array Java’s Iterable Memory Composition Kotlin’s Iterable; Java 8 Streams Rx Observable
  19. 19. Java’s Array Java’s Iterable Memory Composition Kotlin’s Iterable; Java 8 Streams Rx Observable
  20. 20. Java’s Array Java’s Iterable Memory Composition Kotlin’s Iterable; Java 8 Streams Rx Observable
  21. 21. Java’s Array Java’s Iterable Memory Composition Kotlin’s Iterable; Java 8 Streams Rx Observable ??? ???
  22. 22. declaratively composable sequence
  23. 23. declaratively composable sequence
  24. 24. Java’s Array Java’s Iterable Memory Composition Kotlin’s Iterable; Java 8 Streams Rx Observable
  25. 25. Java’s Array Java’s Iterable Memory Composition Kotlin’s Iterable; Java 8 Streams Rx Observable Javascript’s Array
  26. 26. Java’s Array Java’s Iterable Memory Composition Kotlin’s Iterable; Java 8 Streams Rx Observable Javascript’s Array
  27. 27. declaratively composable array
  28. 28. imperatively composable array
  29. 29. Java’s Array Java’s Iterable Memory Composition Kotlin’s Iterable; Java 8 Streams Rx Observable Javascript’s Array
  30. 30. Country[] northAmericanCountries = { 
 new Country("United States", 300e6),
 new Country("Canada", 36e6),
 new Country("Mexico", 127e6),
 //...
 };
  31. 31. double[] getPopulations(Country[] counties) {
 double[] populations = new double[counties.length];
 for (int i = 0; i < counties.length; i++) {
 populations[i] = counties[i].getPopulation();
 }
 return populations;
 }
  32. 32. double[] getPopulations(Country[] counties) {
 double[] populations = new double[counties.length];
 for (int i = 0; i < counties.length; i++) {
 populations[i] = counties[i].getPopulation();
 }
 return populations;
 }
  33. 33. double[] getPopulations(Country[] counties) {
 double[] populations = new double[counties.length];
 for (int i = 0; i < counties.length; i++) {
 populations[i] = counties[i].getPopulation();
 }
 return populations;
 }
  34. 34. double[] getPopulations(Country[] counties) {
 double[] populations = new double[counties.length];
 for (int i = 0; i < counties.length; i++) {
 populations[i] = counties[i].getPopulation();
 }
 return populations;
 }
  35. 35. Java’s Array Java’s Iterable Memory Composition Kotlin’s Iterable; Java 8 Streams Rx Observable Javascript’s Array
  36. 36. Java’s Array Java’s Iterable Memory Composition Kotlin’s Iterable; Java 8 Streams Rx Observable Javascript’s Array
  37. 37. const countries = [ { name: "United States", population: 300e6 }, { name: "Canada", population: 36e6 }, { name: "Mexico", population: 107e6 } ];
  38. 38. const getPopulations = (countries) => countries.map(it => it.population);
  39. 39. class Array { map(transform) { const newList = []; for (let i = 0; i < this.size; i++) { newList.push(transform(this[i])); } return newList; } }
  40. 40. map
  41. 41. map filter
  42. 42. map filter reduce
  43. 43. const northAmericanPopExcludingUs = countries .filter(it => it.name === "United States") .map(it => it.population) .reduce((totalPop, countryPop) => totalPop + countryPop);
  44. 44. const northAmericanPopExcludingUs = countries .filter(it => it.name === "United States") .map(it => it.population) .reduce((totalPop, countryPop) => totalPop + countryPop);
  45. 45. const northAmericanPopExcludingUs = countries .filter(it => it.name === "United States") .map(it => it.population) .reduce((totalPop, countryPop) => totalPop + countryPop);
  46. 46. const northAmericanPopExcludingUs = countries .filter(it => it.name === "United States") .map(it => it.population) .reduce((totalPop, countryPop) => totalPop + countryPop);
  47. 47. const northAmericanPopExcludingUs = countries .filter(it => it.name === "United States") .map(it => it.population) .reduce((totalPop, countryPop) => totalPop + countryPop);
  48. 48. const northAmericanPopExcludingUs = countries .filter(it => it.name === "United States") .map(it => it.population) .reduce((totalPop, countryPop) => totalPop + countryPop);
  49. 49. 300e6 + 36e6 + 127e6 + …
  50. 50. declaratively composable array
  51. 51. Java’s Array Java’s Iterable Memory Composition Kotlin’s Iterable; Java 8 Streams Rx Observable Javascript’s Array
  52. 52. map filter reduce
  53. 53. map filter reduce + LinkedList
  54. 54. map filter reduce + LinkedList = 💥
  55. 55. How is the sequence stored in memory?
  56. 56. Java’s Array Java’s Iterable Memory Composition Kotlin’s Iterable; Java 8 Streams Rx Observable Javascript’s Array
  57. 57. Java’s Array Java’s Iterable Memory Composition Kotlin’s Iterable; Java 8 Streams Rx Observable Javascript’s Array
  58. 58. Array
  59. 59. Array LinkedList
  60. 60. Array LinkedList Iterable
  61. 61. Country[] northAmericanCountries = { 
 new Country("United States", 300e6),
 new Country("Canada", 36e6),
 new Country("Mexico", 127e6),
 //...
 };
  62. 62. for (int i = 0; i < northAmericanCountries.length; i++) {
 System.out.println(northAmericanCountries[i]);
 }
  63. 63. for (int i = 0; i < linkedList.length; i++) {
 System.out.println(linkedList[i]);
 }
  64. 64. for (final Object object : array) {
 System.out.println(object);
 }
  65. 65. for (final Object object : linkedList) {
 System.out.println(object);
 }
  66. 66. for (final Object object : iterable) {
 System.out.println(object);
 }
  67. 67. How is the sequence stored in memory?
  68. 68. Java’s Array Java’s Iterable Memory Composition Kotlin’s Iterable; Java 8 Streams Rx Observable Javascript’s Array
  69. 69. Java’s Array Java’s Iterable Memory Composition Kotlin’s Iterable; Java 8 Streams Rx Observable Javascript’s Array
  70. 70. Java’s Array Java’s Iterable Memory Composition Kotlin’s Iterable; Java 8 Streams Rx Observable Javascript’s Array
  71. 71. declaratively composable iterable
  72. 72. fun getPopulations(countries: Iterable<Country>)
 = countries.map { it.population }
  73. 73. fun getPopulations(countries: Iterable<Country>)
 = countries.map { it.population }
  74. 74. fun <T, R> Iterable<T>.map(transform: (T) -> R): List<R> {
 val destination = ArrayList<R>(collectionSizeOrDefault(10))
 for (item in this)
 destination.add(transform(item))
 return destination
 }
  75. 75. fun <T, R> Iterable<T>.map(transform: (T) -> R): List<R> {
 val destination = ArrayList<R>(collectionSizeOrDefault(10))
 for (item in this)
 destination.add(transform(item))
 return destination
 }
  76. 76. fun getTotalPopulationExcludingUs(countries: Iterable<Country>)
 = countries
 .filter { it.name == "United States" }
 .map { it.population }
 .reduce { total, currentPop -> total + currentPop }
  77. 77. fun getTotalPopulationExcludingUs(countries: Iterable<Country>)
 = countries
 .filter { it.name == "United States" }
 .map { it.population }
 .reduce { total, currentPop -> total + currentPop }
  78. 78. fun getTotalPopulationExcludingUs(countries: Iterable<Country>)
 = countries
 .filter { it.name == "United States" }
 .map { it.population }
 .reduce { total, currentPop -> total + currentPop }
  79. 79. fun getTotalPopulationExcludingUs(countries: Iterable<Country>)
 = countries
 .filter { it.name == "United States" }
 .map { it.population }
 .reduce { total, currentPop -> total + currentPop }
  80. 80. 300e6 + 36e6 + 127e6 + …
  81. 81. declaratively composable iterable
  82. 82. Java’s Array Java’s Iterable Memory Composition Kotlin’s Iterable; Java 8 Streams Rx Observable Javascript’s Array
  83. 83. Java’s Array Java’s Iterable Memory Composition Kotlin’s Iterable; Java 8 Streams Rx Observable Javascript’s Array
  84. 84. declaratively composable sequence
  85. 85. declaratively composable sequence
  86. 86. button.setOnClickListener(new View.OnClickListener() {
 @Override public void onClick(View v) {
 trackClick();
 }
 });
  87. 87. for (int buttonClick : buttonClicks) {
 trackClick();
 } button.setOnClickListener(new View.OnClickListener() {
 @Override public void onClick(View v) {
 trackClick();
 }
 });
  88. 88. for (int buttonClick : buttonClicks) {
 trackClick();
 } button.forEachClick(new View.OnClickListener() {
 @Override public void onClick(View v) {
 trackClick();
 }
 });
  89. 89. for (int buttonClick : buttonClicks) {
 trackClick();
 } clickSequence.forEachClick(new View.OnClickListener() {
 @Override public void onClick(View v) {
 trackClick();
 }
 });
  90. 90. clickSequence.forEachClick(new View.OnClickListener() {
 @Override public void onClick(View v) {
 trackClick();
 }
 });
  91. 91. clickObservable.subscribe(new Consumer<View> {
 @Override public void accept(View v) {
 trackClick();
 }
 });
  92. 92. declaratively composable sequence
  93. 93. declaratively composable sequence
  94. 94. declaratively composable iterable
  95. 95. map filter reduce
  96. 96. map filter reduce + LinkedList
  97. 97. map filter reduce + LinkedList = 💥
  98. 98. How is the sequence stored in memory?
  99. 99. Iterable
  100. 100. How is the sequence stored in memory?
  101. 101. map filter reduce
  102. 102. map filter reduce + Async
  103. 103. map filter reduce + Async = 💥
  104. 104. Is the sequence currently in memory?
  105. 105. Observable
  106. 106. Is the sequence currently in memory?
  107. 107. Array
  108. 108. Array LinkedList
  109. 109. Array LinkedList Iterable
  110. 110. Array LinkedList Iterable …
  111. 111. Array LinkedList Iterable …
  112. 112. Array LinkedList Iterable … Sequence
  113. 113. Array LinkedList Iterable … Sequence
  114. 114. Array LinkedList Iterable … Observable
  115. 115. declaratively composable sequence
  116. 116. [k, ke, key, keyn, keyno, keynot, keynote] [results for ‘k’, results for ‘ke', results for ‘key’…] for each result, update UI
  117. 117. makeQueryTextObservable(mSearchView)
 .flatMap { s -> makeLoadObservable(s) }
 .subscribe { cursor ->
 mResultsAdapter.swapCursor(cursor)
 }
  118. 118. makeQueryTextObservable(mSearchView)
 .flatMap { s -> makeLoadObservable(s) }
 .subscribe { cursor ->
 mResultsAdapter.swapCursor(cursor)
 }
  119. 119. [k, ke, key, keyn, keyno, keynot, keynote] makeQueryTextObservable(mSearchView)
 .flatMap { s -> makeLoadObservable(s) }
 .subscribe { cursor ->
 mResultsAdapter.swapCursor(cursor)
 }
  120. 120. makeQueryTextObservable(mSearchView)
 .flatMap { s -> makeLoadObservable(s) }
 .subscribe { cursor ->
 mResultsAdapter.swapCursor(cursor)
 }
  121. 121. makeQueryTextObservable(mSearchView)
 .flatMap { s -> makeLoadObservable(s) }
 .subscribe { cursor ->
 mResultsAdapter.swapCursor(cursor)
 }
  122. 122. makeQueryTextObservable(mSearchView)
 .flatMap { s -> makeLoadObservable(s) }
 .subscribe { cursor ->
 mResultsAdapter.swapCursor(cursor)
 } [results for ‘k’, results for ‘ke', results for ‘key’…]
  123. 123. makeQueryTextObservable(mSearchView)
 .flatMap { s -> makeLoadObservable(s) }
 .subscribe { cursor ->
 mResultsAdapter.swapCursor(cursor)
 }
  124. 124. makeQueryTextObservable(mSearchView)
 .flatMap { s -> makeLoadObservable(s) }
 .subscribe { cursor ->
 mResultsAdapter.swapCursor(cursor)
 }
  125. 125. makeQueryTextObservable(mSearchView)
 .flatMap { s -> makeLoadObservable(s) }
 .subscribe { cursor ->
 mResultsAdapter.swapCursor(cursor)
 }
  126. 126. makeQueryTextObservable(mSearchView) .filter { s -> s.length > 3 }
 .flatMap { s -> makeLoadObservable(s) }
 .subscribe { cursor ->
 mResultsAdapter.swapCursor(cursor)
 }
  127. 127. makeQueryTextObservable(mSearchView) .filter { s -> s.length > 3 }
 .flatMap { s -> makeLoadObservable(s) }
 .subscribe { cursor ->
 mResultsAdapter.swapCursor(cursor)
 }
  128. 128. [k, ke, key, keyn, keyno, keynot, keynote] filter [keyn, keyno, keynot, keynote]
  129. 129. makeQueryTextObservable(mSearchView) .filter { s -> s.length > 3 }
 .flatMap { s -> makeLoadObservable(s) }
 .subscribe { cursor ->
 mResultsAdapter.swapCursor(cursor)
 }
  130. 130. makeQueryTextObservable(mSearchView!!)
 .filter { s -> s.length > 3 }
 .debounce(300, TimeUnit.MILLISECONDS)
 .flatMap { s -> makeLoadObservable(s) }
 .subscribe { cursor ->
 mResultsAdapter!!.swapCursor(cursor)
 }
  131. 131. makeQueryTextObservable(mSearchView!!)
 .filter { s -> s.length > 3 }
 .debounce(300, TimeUnit.MILLISECONDS)
 .flatMap { s -> makeLoadObservable(s) }
 .subscribe { cursor ->
 mResultsAdapter!!.swapCursor(cursor)
 }
  132. 132. makeQueryTextObservable(mSearchView!!)
 .filter { s -> s.length > 3 }
 .debounce(300, TimeUnit.MILLISECONDS)
 .flatMap { s -> makeLoadObservable(s) }
 .subscribe { cursor ->
 mResultsAdapter!!.swapCursor(cursor)
 }
  133. 133. makeQueryTextObservable(mSearchView!!)
 .filter { s -> s.length > 3 }
 .debounce(300, TimeUnit.MILLISECONDS)
 .flatMap { s -> makeLoadObservable(s) }
 .subscribeOn(Schedulers.io())
 .observeOn(AndroidSchedulers.mainThread())
 .subscribe { cursor ->
 mResultsAdapter!!.swapCursor(cursor)
 }
  134. 134. makeQueryTextObservable(mSearchView!!)
 .filter { s -> s.length > 3 }
 .debounce(300, TimeUnit.MILLISECONDS)
 .flatMap { s -> makeLoadObservable(s) }
 .subscribeOn(Schedulers.io())
 .observeOn(AndroidSchedulers.mainThread())
 .subscribe { cursor ->
 mResultsAdapter!!.swapCursor(cursor)
 }
  135. 135. makeQueryTextObservable(mSearchView!!)
 .filter { s -> s.length > 3 }
 .debounce(300, TimeUnit.MILLISECONDS)
 .flatMap { s -> makeLoadObservable(s) }
 .subscribeOn(Schedulers.io())
 .observeOn(AndroidSchedulers.mainThread())
 .subscribe { cursor ->
 mResultsAdapter!!.swapCursor(cursor)
 }
  136. 136. declaratively composable sequence
  137. 137. An Introduction to RxJava https://twitter.com/philosohacker https://twitter.com/unikeytech

×