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

Getting Started with RxJava

Getting Started with RxJava

Soham Mondal

October 17, 2015
Tweet

More Decks by Soham Mondal

Other Decks in Technology

Transcript

  1. Problems with AysncTask - Generic Android AsyncTask issues of course

    - Parsing data? Filtering? Caching? - Error handling/Retry - Control over threads - There is not enough time or space to discuss this!
  2. Travel App - Find location Asynchronously - Find user details

    Asynchronously - Once you have location, make a network call for a list of all hotels in the area for that user - Make a separate api call for each hotel to check for a) occupancy and b) price and c) ratings - Dynamically filter hotels based on preference - Update data as soon as it arrives
  3. About RxJava - Rx: Reactive Extensions - Functional Reactive programming

    - From the good folks at Microsoft - Erik Meijer on the Microsoft .NET platform - Notoriously difficult to approach - also on .net, javascript, scala, kotlin, clojure, you get the picture
  4. Functional Programming In computer science, functional programming is a programming

    paradigm—a style of building the structure and elements of computer programs—that treats computation as the evaluation of mathematical functions and avoids changing-state and mutable data. It is a declarative programming paradigm, which means programming is done with expressions. In functional code, the output value of a function depends only on the arguments that are input to the function, so calling a function f twice with the same value for an argument x will produce the same result f(x) each time. Eliminating side effects, i.e. changes in state that do not depend on the function inputs, can make it much easier to understand and predict the behavior of a program, which is one of the key motivations for the development of functional programming.
  5. Reactive Programming In computing, reactive programming is a programming paradigm

    oriented around data flows and the propagation of change. This means that it should be possible to express static or dynamic data flows with ease in the programming languages used, and that the underlying execution model will automatically propagate changes through the data flow. For example, in an imperative programming setting, a := b + c would mean that a is being assigned the result of b + c in the instant the expression is evaluated. Later, the values of b and c can be changed with no effect on the value of a.
  6. Why RxJava? Lesser Cognitive load Code is simpler and much

    more readable Complex operations made easy Operators help do complex tasks like filtering Easy threading Threading in ridiculously simple Easy error handling Error handling is just better Easy to test Unit testing your apps is simple
  7. The Basics Items/Stream Things that are generated or emitted Observable

    An observable emits zero or more items Subscriber A subscriber consumes those items
  8. Item/Stream A stream of items that you are interested in!

    Example: - A network response - A database response - An android view update
  9. Item/Stream A stream of items that you are interested in!

    Example: - A network response - A database response - An android view update
  10. Observable Observables emit items/stream. In an observable you would typically:

    - Determine how to emit the items/stream - Determine how to emit an error
  11. Creating Observables - Observable.create - Observable.just - Observable.from - More

    at https://github. com/ReactiveX/RxJava/wiki/Cr eating-Observables
  12. Retrofit - Native support for RxJava - Can return Observables

    instead of Callbacks/Synchronous methods
  13. Zip - combine sets of items emitted by two or

    more Observables together via a specified function and emit items based on the results of this function - http://reactivex. io/documentation/operat ors/zip.html
  14. Other operators - combineLatest - merge - timeout - first,

    last - https://github.com/ReactiveX/RxJava/wiki/Alphabetical-List-of- Observable-Operators
  15. Sample Scenario - I Things to do Get a list

    of albums Display the results
  16. Sample Scenario - II Things to do Get a list

    of albums for Metallica Get a list of albums for Iron Maiden Combine the results Sort the list Display the results