This article will help you learn RxJava 2.0 by example. It will also show you how to migrate from RxJava 1.0.
If you are just getting starting with RxJava, here’s a sample project to learn it with examples. You can dive right in, build the project, and start learning:
Check out Fast Android Networking Library which supports RxJava2.
RxJava 2.0 has been completely rewritten from scratch on top of the Reactive-Streams specification. This specification itself has evolved out of RxJava 1.x, and provides a common baseline for reactive systems and libraries.
Because Reactive-Streams has a different architecture, it mandates changes to some well known RxJava types.
Here’s what’s changed from RxJava 1.0:
- onCompleted became onComplete — without the trailing d
- Func1 became Function
- Func2 became BiFunction
- CompositeSubscription became CompositeDisposable
- limit operator has been removed. Use take in RxJava 2.
If you look at this project, you’ll find the following:
- An example using CompositeDisposable, since CompositeSubscription and Subscription have been removed.
- An example using Flowable.
- An example using SingleObserver and CompletableObserver.
- An example using RxJava2 operators such as map, zip, take, reduce, flatMap, filter, buffer, and much more.
- An example using Function since Func1 has been removed.
- An example using BiFunction since Func2 has been removed.
- And many others examples.
Here are a few important RxJava operators that are covered in the project, and what they do:
- Map transforms the items emitted by an Observable by applying a function to each item
- Zip combines the emissions of multiple Observables together via a specified function, then emits a single item for each combination based on the results of this function
- Filter emits only those items from an Observable that pass a predicate test
- FlatMap transforms the items emitted by an Observable into Observables, then flattens the emissions from those into a single Observable
- Take emits only the first n items emitted by an Observable
- Reduce applies a function to each item emitted by an Observable, sequentially, and emits the final value
- Skip suppresses the first n items emitted by an Observable
- Buffer periodically gathers items emitted by an Observable into bundles and emits these bundles rather than emitting the items one at a time
- Concat emits the emissions from two or more Observables without interleaving them
- Replay ensures that all observers see the same sequence of emitted items, even if they subscribe after the Observable has begun emitting items
- Merge combines multiple Observables into one by merging their emissions
RxJava is a Java VM implementation of Reactive Extensions. It has become the single most important skill for Android development.
Happy Coding 🙂