Pages

January 11, 2019

#RxJava Part 1

Reactive Programming
  • In reactive programming, we react to changes in the state instead of actually doing the state change.
  • The reactive model listens to changes in the event and runs the relevant code accordingly.
  • e.g if in an excel file we have three columns A, B and C. In the C column we added a formula because of which value in C is A+B. All the rows of column C is populated with the summation of values in A and B. Now if we change the formula from A+B to A-B, it will automatically change the values in all the rows of column C.
  • Reactive Programming is a programming paradigm that’s concerned with data streams and propagation of change.
  • Assume the data streams are in the form of a river that flows continuously. Any observer/subscriber attached listening to the stream would receive the data. The data received can be further transformed using functions and this is where Functional Programming joins the already so powerful Reactive Programming.
  • In reactive programming, the flow is asynchronous thereby preventing any blocks on the main thread.

How Reactive Programming is different from the Observer Pattern?
Observer Pattern does changes in the object based on certain triggers. Every Observer that’s dependent on the Subject gets notified when a change happens. What makes reactive programming different from this is the fact that reactive paradigm is based on the flow of data in the form of streams asynchronously.

What is RxJava?
  • Rx stands for Reactive Extensions and RxJava is a JVM implementation of Reactive Extensions.
  • Reactive Extension is a library that’s used for writing asynchronous event-based reactive code by using observables.
  • RxJava is useful and very powerful in the sense that it takes care of multithreading very well. RxJava takes care of multi-threading by doing complex thread operations, keeping everything synchronized and returning the relevant things to the main thread.
What are the basic building blocks of RxJava?
  • Observables: That emits data streams. 
  • Observers and Subscribers: That consume the data stream. The only difference between an Observer and a Subscriber is that a Subscriber class has the methods to unsubscribe/resubscribe independently without the need of the observerable methods. We can say, an observer is any object that wishes to be notified when the state of another object changes. 
  • Operators: That transform the data stream
Observables often don’t start emitting items until someone subscribes to them. Subscriber subscribes to Observable, then Observable calls Subscriber.onNext() for any number of items, if something goes wrong Subsciber.onError() is called and if all finishes fine, here is Subscriber.onCompleted().

An observer subscribes to an Observable sequence. The sequence sends items to the observer one at a time. The observer handles each one before processing the next one. If many events come in asynchronously, they must be stored in a queue or dropped.

In Rx, an observer will never be called with an item out of order or called before the callback has returned for the previous item.

Types of Observable
There are basically two types of Observable:
Non-Blocking: They support asynchronous execution and is allowed to unsubscribe at any point in the event stream.

Blocking: All onNext observer calls will be synchronous, and it is not possible to unsubscribe in the middle of an event stream. We can always convert an Observable into a Blocking Observable, using the toBlocking() method. e.g:

BlockingObservable blockingObservable = observable.toBlocking();

-K Himaanshu Shuklaa..

No comments:

Post a Comment