22

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.

paul
  • 12,873
  • 23
  • 91
  • 153
  • 2
    I would say that the unique difference is that it uses the new [Java Streams API](https://docs.oracle.com/javase/8/docs/api/java/util/stream/package-summary.html) that was introduced in Java 8. – Andreas Jul 17 '16 at 15:57
  • So then having Java 8 what's the point? Is any different the Java 8 stream and rxjava 2 stream? – paul Jul 17 '16 at 16:05
  • 1
    Did you even read the second link you provided? *"RxJava 1.x does not directly implement the Reactive Streams APIs"* and *"RxJava 2.x will target Reactive Streams APIs directly"*. That's sounds like a big difference to me: Changing it from not implementing the API directly to actually implementing the API directly. – Andreas Jul 17 '16 at 17:02
  • 1
    Yes I read that, but I just want to be sure if there was any other diferences – paul Jul 17 '16 at 17:06
  • 1
    Your initial assumption in the question was that the only change was that it requires Java 8+. How is that anywhere *close* to the same as it chaning the API? You're not making any sense. What do you want? A minute detailed list of every change made? Compare the source code to see that. – Andreas Jul 17 '16 at 17:11
  • Basically I just want to know if there was any new feature on rxjava2 that is not on RxJava1 but if you tell and you're sure that there's not any other differences, I will appreciate and I will close this ticket. – paul Jul 17 '16 at 17:13
  • 1
    I'm not saying that, since I don't know. I was simply saying that the difference is *way more* than just requiring Java 8. If that wasn't your question, you should modify your question to say what you actually mean to ask. – Andreas Jul 17 '16 at 17:15

3 Answers3

30

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.

akarnokd
  • 69,132
  • 14
  • 157
  • 192
24

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 ]

Dr.jacky
  • 3,341
  • 6
  • 53
  • 91
Amit Shekhar
  • 3,135
  • 2
  • 16
  • 17
2

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 */);
GraSim
  • 3,830
  • 1
  • 29
  • 35