Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 741
paul Avatar asked Jul 17 '16 15:07

paul


People also ask

What is difference between RxJava and RxKotlin?

RxJava is a Java VM implementation of Reactive Extensions. where we can create asynchronous data stream on any thread, transform it and these asynchronous data streams can be consumed by Observers on any thread. What is RxKotlin? RxKotlin is a Kotlin implementation of Reactive Extensions.

What is the difference between coroutines and RxJava?

RxJava can be used with any Java-compatible language, whereas Kotlin coroutines can only be written in Kotlin. This is not a concern for Trello Android, as we are all-in on Kotlin, but could be a concern for others. (Note that this just applies to writing code, not consuming it.

What is the difference between single and Observable in RxJava?

Single and Completable are new types introduced exclusively at RxJava that represent reduced types of Observable , that have more concise API. Single represent Observable that emit single value or error. You can think of the differences like the differences of a method that returns: Collection of Objects - Observable.

What is RxJava used for?

RxJava is a Java library that enables Functional Reactive Programming in Android development. It raises the level of abstraction around threading in order to simplify the implementation of complex concurrent behavior.


3 Answers

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.

like image 129
akarnokd Avatar answered Oct 11 '22 05:10

akarnokd


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 ]

like image 41
Amit Shekhar Avatar answered Oct 11 '22 04:10

Amit Shekhar


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 */);
like image 2
GraSim Avatar answered Oct 11 '22 03:10

GraSim