If you are using Java RX, chances are, like us, you’ve already fallen in love with the reactive world which the functional programming introduces into boring, static code.
We’ve been working with RxJava for the last year and a half and everything’s been great. Last October, however, saw the release of the 2nd generation of RxJava to the World.
This short article introduces what has changed and some advice on how to adopt RxJava2.
First off, it isn’t backward compatible with RxJava. There are some great benefits which we will discuss next, but if you have a large code base in RxJava you might want to consider whether to upgrade it or wait until your next project.
So what's new?
In the new library version, the creators decided to add more detail to work with types. The Observable and the Subscriber have been better thought out. New types have appeared, such as Flowable, Maybe and Nothing and the work with the library has become more evident.
Observable lost its ability to handle MissingBackpressureException. The ability to create Your Observable from null has disappeared.
The ability to use true/1 still remains, but it's better to look at Completable or Obsetvable.fromCallable().
A new type was introduced. Flowable which is separated from the old Observable. Now Observable cannot handle MissingBackpressureException. And if your Subscriber may not have enough time to process data, you will have to use Flowable. The main feature of this type is that during the work you have to point out the handling strategy of MissingBackpressureException.
The example of creating Flowable:
Single and Maybe
Single defines the flow which consists of one element. If the flow is empty – NoSuchElementExction occurs. In this case the type Maybe will be useful, which can return the empty flow.
Also the first() operator’s behavior changed .:
The subscriber has also changed. In its interface there appeared one more method in which the subscription delivers.
The new interface added the ability to quickly cancel the subscription.
The old Composite Subscription was changed to CompositeDisposable:
The new library version has the changed which aids distinctness while writing the reactive flows. The readability of the code has improved and due to the new types appearing the amount of thrown exceptions has diminished.
The lack of backward compatibility library is a pain so we would recommend that you start using it with a brand-new project.