Reputation: 192
I am a newbie to RxJava and planning on integrating it to my app. What are the advantages on using RxJava over EventBus? EventBus decouples classes from traditional callbacks and communication happens over the EventBus. EventBus also provides thread handling capability by giving options to Subscribe on Main or worker thread. What other additional features are provided by Rxjava compared to EventBus?
Upvotes: 8
Views: 2524
Reputation: 3282
EventBus library is supposed to be used for organizing horizontal interaction between Android components or part of complex algorithms when passing references to parts, affected by algorithms, become too expensive. Unfortunately, it break incapsulation a bit, because all subscribe methods must be public.
RxJava library was created by Netflix company to bring reactive programming to Android and it is generalization of 'Observer' design pattern. Its main purpose - represent all incoming and outgoing data as stream of events. Algorithm itself become 'pipeline', mapping incoming and outgoing events. Common entities in rxJava: Observable<>, Subject<>, Subscription, Subscriber.
Observable represent source of events, Subject<> and all derived classes - "Vortex", which can receive some object from regular code and emit in from Observable. Subscriber is a receiver of data, connected to some Observable. Subscription - is a connection between data source (Observable<>) and receiver of those data (Subscriber). When subscription is active, data can flow from source to receiver, when you call .unsubscribe() on subscription, this disconnects source and receive.
The rest of RxJava is a load of operators for organizing data flow from source to destination.
So, those two libraries serve a different purpose.
It worth to notice, that you can organize execution in background by using rxJava library by using .subscribeOn() and .observeOn() operators (see docs)
Upvotes: 2
Reputation: 4077
in my experience, bus does not play well with (Android) lifecycle, i.e. events were sent when there was no one to receive them, and once relevant components were created events were long gone.
Upvotes: 1
Reputation: 13471
Those two are completely different topics/technologies.
Event bus, is just a JMS system to communicate services using publisher/subscriber pattern as ActiveMQ or tibco does.
And RXJava is an extension to apply observer pattern asynchronously if you need.
As you can see both are completely different solutions for different needs.
You can read some good documentation in Vertx Tool which implement EvemntBus http://vertx.io/docs/vertx-core/java/#event_bus
And for RxJava you can see in this project multiple practical examples about how this library and his operators works. https://github.com/politrons/reactive
Welcome to the reactive world!
Upvotes: 3
Reputation: 19351
Although I am also newbie and I haven't written already any piece of code in RxJAva I would do my best to explain it to you.
The main difference between them is that RxJava
means Reactive Extensions for Java, which means nothing than writing Java code in reactive way, where reactive is programming paradigm like object-oriented programming and it's based on functional programming. It represents all incoming and outgoing data as stream of events.
To make it more clear, have a look at classic Hello World
implementation using RxJava:
public static void hello(String... names) { Observable.from(names).subscribe(new Action1<String>() { @Override public void call(String s) { System.out.println("Hello " + s + "!"); } }); }
From: https://github.com/ReactiveX/RxJava/wiki/How-To-Use-RxJava
and at definition taken from its official Github page:
Reactive Extensions for the JVM – a library for composing asynchronous and event-based programs using observable sequences for the Java VM.
It extends the observer pattern to support sequences of data/events and adds operators that allow you to compose sequences together declaratively while abstracting away concerns about things like low-level threading, synchronization, thread-safety and concurrent data structures.
So if you want to write code in reactive way it's recommended to learn object, then functional (there's a great introduction course on Coursera or try to learn Kotlin), finally choose reactive.
EventBus is just a small library to simplify communication between Activities, Fragments, Threads, Services, etc. It lacks of maaaany features, which RxJava provides. Futhermore, event bus can be pretty simply created with RxJava, so if you you use RxJava in your project, you would forget about EventBus or Otto libraries.
Take a look at: http://blog.kaush.co/2014/12/24/implementing-an-event-bus-with-rxjava-rxbus/
So it isn't as you already notice choose one or another library. If you need only to provide event between Activities, in most cases you would do not use RxJava as it is pretty heavy.
Upvotes: 2