paul
paul

Reputation: 13471

Differences between RxJava1 and RxJava2

I've been looking the documentations of RxJava1 https://github.com/ReactiveX/RxJava/releases and RxJava2 https://github.com/ReactiveX/RxJava/wiki/Reactive-Streams and seems like the unique different is that RxJava 2 has Java Stream.

Any other different?.

I've been working with Version 1.1.3 but I'm not sure if it's worth it move to RxJava2 since we're already using Java 8 streams in our code

Regards.

Upvotes: 22

Views: 11411

Answers (3)

GraSim
GraSim

Reputation: 4210

One of the major differences applies to the .filter operator . As stated by the docs:

In addition, operators requiring a predicate no longer use Func1<T, Boolean> but have a separate, primitive-returning type of Predicate<T> (allows better inlining due to no autoboxing).

So for the .filter operator , you will need to change like the example below

        RxTextView.textChanges(editText)
            .debounce(400, TimeUnit.MILLISECONDS)
            .filter(new Predicate<CharSequence>() {
                @Override
                public boolean lengthOk(CharSequence charSequence) {
                    return charSequence.length() > 3;
                }
            })
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(/* attach your observer */);

Upvotes: 2

Amit Shekhar
Amit Shekhar

Reputation: 3195

As I have implemented RxJava2 in my sample project - link

The following the differences between RxJava2 and RxJava1 :

  1. To allow having RxJava 1.x and RxJava 2.x side-by-side, RxJava 2.x is under the maven coordinates io.reactivex.rxjava2:rxjava:2.x.y and classes are accessible below io.reactivex.

  2. Users switching from 1.x to 2.x have to re-organize their imports, but carefully.

  3. onCompleted -> onComplete - without the trailing d

  4. CompositeSubscription -> CompositeDisposable - CompositeDisposable as CompositeSubscription and Subscription have been removed

  5. Func1 -> Function

  6. Func2 -> BiFunction

  7. limit operator has been removed - Use take in RxJava2

RxJava 2.0 has been completely rewritten from scratch on top of the Reactive-Streams specification. The 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.

RxJava2 has better performance and low memory usage over RxJava1

[Source: https://github.com/ReactiveX/RxJava/wiki/What%27s-different-in-2.0 ]

Upvotes: 24

akarnokd
akarnokd

Reputation: 69997

Both RxJava 1.x and 2.x are designed to be Java 6+ and thus we can't support Java 8 Streams in any of the versions. This was decided to keep support the myriad of Android devices and versions which won't ever get updated to a Java 8 compatible runtime. If you need Java 8 support, consider using Reactor-Core from Pivotal.

The major difference between the two is that 2.x targets the Reactive-Streams SPI directly and for this, it has been completely rewritten from scratch. We are currently in development preview mode which you can access as described in the 2.x branch readme.

The complete rewrite of 2.x improved our memory consumption and performance considerably; here is a benchmark that compares various versions and libraries.

On the API surface, we plan to keep supporting all operators that are present in 1.x and likely extend both versions with new ones for a few years before support on 1.x ends.

Since 2.x is a new architecture, many depending libraries (e.g., Retrofit) has to be updated as well; which likely won't happen earlier than end of this August or may as well take several months to catch up. Here is the wiki page that contains the highlights of the differences.

Upvotes: 30

Related Questions