Reactive Programming on Android

But First

Observer Pattern

Observer Pattern

"The observer pattern is a software design pattern in which an object, called the subject, maintains a list of its dependents, called observers, and notifies them automatically of any state changes, usually by calling one of their methods."

Observer Pattern

Enter Reactive Programming

"Programming paradigm oriented around data flows and the propagation of change."

AKA The Excel Pattern

  • You create a formula A1+B1
  • A1 value is updated
  • The formula is automagically updated
  • Pull vs Push

RxJava extends the Observer pattern

  • Observable
  • Subscriber
  • Subject

Data flow

Observable - Subscriber

But Why?????

Concurrency is hard

  • "Multithreading is always complex"
  • "Callbacks have their own problems"
  • "Java Futures are expensive to compose"

Ben Christensen from Netflix

Simplify Concurrency

  • Turn everything into a stream of data
  • Our business logic now simply "reacts" to the stream of data
  • We get multi threading for free, we now simply tell in which thread our operations need to happen

Enough Talk

Show me the Code!

Sample 1

  • Get a list of items from a web service and show them on the screen
  • Simple app with only one Activity
  • We will use Github API to get a list of repos for a user

Wow that was amazing!

Pros

  • No memory leak
  • Cleaner error handling

Cons

  • More code to write than the non reactive app

The beauty of RxJava comes when we use operators

Operators: Filter

Operators: First

Emit only the first item (or the first item that meets some condition) emitted by an Observable

Operators: Concat

Emit the emissions from two or more Observables without interleaving them

Operators:Debounce

Only emit an item from an Observable if a particular timespan has passed without it emitting another item

Operators: FlatMap

Transform the items emitted by an Observable into Observables, then flatten the emissions from those into a single Observable

Sample 2

  • Implement a "search as you type" interface
  • The user enters a search term into a text field and the app must start showing the results for that search after the user has finished typing
  • A valid search must have more than 3 characters
  • We will use Github's search API to search repositories by their name

Sample 3

  • Implement a two level cache for offline viewing of data fetched from a web service
  • First Level: the data must be cached to disk
  • Second Level: the data must be cached in memory
  • And finally after a period of time new data must be fetched from the server and the cache needs to be refreshed

Conclusions

  • As always a new library/framework must be chosen carefully depending on your particular requirements
  • RxJava allows you to think in terms of sources of data and operations which simplifies a lot of complex use cases

Thank you!

References

Made with Slides.com