-
Notifications
You must be signed in to change notification settings - Fork 6.3k
RxJava
RxJava is a library used to help write in a style known as reactive programming. Reactive programming often requires thinking of everything in the context of processing asynchronous data streams, whether they are network calls, button clicks, or search text changes. By defining the relationship of these data streams should be computed instead of specifying how the values should be calculated, this approach marks a huge departure from traditional imperative programming models.
One of the best introductions about reactive programming is described [here] (https://gist.github.com/staltz/868e7e9bc2a7b8c1f754). There is a mental shift that must be made in thinking about how asynchronous data streams can be used to transform into other data streams, filtered into a new stream, or used to create new streams. It sometimes is helpful to imagine these data streams along a time continuum whereby certain producers emit values and downstream subscribers respond to these changes.
Here are a few advantages of using RxJava on Android:
-
Simplifies the ability to chain async operations. If you need to make an API call that depends on another API call, you will likely end up implementing this call in the callback of the first one, as well as the logic needed to check whether the first API call succeeded and whether to dispatch the next one. RxJava provides a way to avoid callback hell. For this reason, RxJava became popular within Netflix in 2014 for abstracting away the complexities of performing dependent API calls.
-
Exposes a more flexible way for declaring how concurrent operations should operate. One issue with AsyncTask is that errors that occur on the background thread are hard to pass along when updating the UI thread using the
onPostExecute()
method. In addition, there are limitations in how many AsyncTasks can be dispatched concurrently as described in this blog post. Not only can RxJava enables errors to be propagated, but the library also allows for what type of threading model can be used for both background and callback tasks. -
Provides powerful constructs for manipulating streams of data. With only a few lines of code, we can write code to aggregate data from different web service calls and filter, merge, or sort them with only a few lines of code.
Setup your app/build.gradle
:
dependencies {
compile 'io.reactivex:rxjava:1.0.16'
compile 'io.reactivex:rxandroid:1.0.1'
}
- https://github.com/ReactiveX/RxJava/wiki/The-RxJava-Android-Module
- http://saulmm.github.io/when-Iron-Man-becomes-Reactive-Avengers2/
- http://blog.stablekernel.com/replace-asynctask-asynctaskloader-rx-observable-rxjava-android-patterns/
- https://www.youtube.com/watch?v=_t06LRX0DV0/
- https://vimeo.com/144812843
Created by CodePath with much help from the community. Contributed content licensed under cc-wiki with attribution required. You are free to remix and reuse, as long as you attribute and use a similar license.
Finding these guides helpful?
We need help from the broader community to improve these guides, add new topics and keep the topics up-to-date. See our contribution guidelines here and our topic issues list for great ways to help out.
Check these same guides through our standalone viewer for a better browsing experience and an improved search. Follow us on twitter @codepath for access to more useful Android development resources.