Back

RxJava2 – how not to get lost

Down

Introduction

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

Observable lost its ability to handle MissingBackpressureException. The ability to create Your Observable from null has disappeared.

Observable.just(null); //NullPointerException
Single.just(null); //NullPointerException

The ability to use true/1 still remains, but it's better to look at Completable or Obsetvable.fromCallable().

Flowable

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.

1.jpg

The example of creating Flowable:

Flowable.create((FlowableEmitter<Integer> emitter) -> {
emitter.onNext(1);
emitter.onNext(2);
emitter.onComplete();
}, BackpressureStrategy.BUFFER);

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.

Observable.empty()
.toSingle() //NoSuchElementException
.subscribe();
Observable.empty()
.toMaybe() //Ok
.subscribe();

Also the first() operator’s behavior changed .:

Observable.empty()
.first() //return Single -> NoSuchElementException
.subscribe();
Observable.empty()
.firstElement() //return Maybe -> Ok
.subscribe();

Subscriber

The subscriber has also changed. In its interface there appeared one more method in which the subscription delivers.

Flowable.range(1, 10).subscribe(new Subscriber<Integer>() {
@Override
public void onSubscribe(Subscription s) {
s.request(Long.MAX_VALUE);
}
@Override
public void onNext(Integer t) {
System.out.println(t);
}
@Override
public void onError(Throwable t) {
t.printStackTrace();
}
@Override
public void onComplete() {
System.out.println("Done");
}
});

The new interface added the ability to quickly cancel the subscription.

CompositeSubscription

The old Composite Subscription was changed to CompositeDisposable:

CompositeDisposable composite = new CompositeDisposable();
composite.add(Flowable.range(1, 5).subscribeWith(subscriber));

Conclusion

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.

Article Rating

16 Reviews
4.8 / 5.0

We hope you enjoyed this article! It's very important for us to receive your feedback. You can use these emojis to describe your feelings.

  • 5
  • 4
  • 3
  • 2
  • 1
 
 
 
 
 
 
Request a quote
 
 
 
 
 
 
prev next
Be the first to receive helpful tips from Applikey
Please enter correct email address
Fasten your seat belts, we are taking off