RxJava Flashcards
RxJava
RxJava is a library that allows us to represent any operation as an asynchronous data stream that can be created on any thread, declara‐tively composed, and consumed by multiple objects on any thread.
RxJava is a specific implementation of reactive programming for Java and Android that is influenced by functional programming.
RxJava is a Java VM implementation of Reactive Extensions.
*At the core RxJava implements an publish - subscribe pattern. Observable (the publisher) sends a value and a Observer(Subscriber) consumes it However, the trick is we can modify the values on the way.
Observer Design Pattern
Observer design pattern is useful when you are interested in the state of an object and want to get notified whenever there is any change.
- Observer - the object that watch on the state of another object
- Subject -the object that is being watched
Subject maintains its Observers and notifies them automatically of any state change, usually by calling one of their methods.
*IMPLEMENTATION In Subject we have: - methods to register and unregister observers - method to notify observers of change - method to get updates from subject
In Observer we have;
- a method to attach the Subject to the observer;
- a method to be used by Subject to notify of any change.
Publish–subscribe pattern
publish–subscribe is a messaging pattern where senders of messages, called publishers, do not program the messages to be sent directly to specific receivers, called subscribers, but instead categorize published messages into classes without knowledge of which subscribers, if any, there may be. Similarly, subscribers express interest in one or more classes and only receive messages that are of interest, without knowledge of which publishers, if any, there are.
Main components of RxJava
There are two main components of RxJava:
– Observable: it emits the values (async streams)
– Observer: it consumes the values (async streams)
Observable/Observer pair connects via subscription.
Observer get the emitted values from Observable by subscribing on it.
+ Observables are what allow us to represent
any operation as an asynchronous data stream
+ Observers are objects that can consume the asynchronous data emit‐ted by an Observable.
There can be multiple Observers that are reg‐
istered to receive the data emitted by an Observable.
Observers can react to the data emitted by the Observable in whatever way they want.
Reactive programming
Reactive programming is a general programming term that is focused on reacting to changes, such as data values or events. It can and often is done imperatively. A call‐ back is an approach to reactive programming done imperatively.
Other components of RxJava
All components are as follows:
Flowable, Observable, Single, and Completable - does some work and emit values.
Subsciption - work is going on or completed or is used to cancel.
Operators - Modify Data
Schedulers - Where the work should be done, which thread like main thread, etc.
Subscriber/Disposable - where the response will be sent after work has been completed.
Subscriber vs Observer
a Subscriber is just an Observer that can unsubscribe from the Observable that emits items.
Types of Observables
> > Observable: It emits 0..N elements, and then completes successfully or with an error.
(onsubscribe, onnext, onerror, oncomplete)
> > Flowable: Similar to Observable but with a backpressure strategy. allows to control how fast a source emits items.
(onsubscribe, onnext, onerror, oncomplete)
> > Single: It completes with a value successfully or an error.(doesn’t have onComplete callback, instead onSuccess(val)).
(onsubscribe, onsucces, onerror)
> > Maybe: It completes with/without a value or completes with an error.
(onsubscribe, onsucces, onerror)
> > Completable: It just signals if it has completed successfully or with an error.
(onsubscribe, onsucces, onerror)
Operators
Operators can be used in between the source Observable and the ultimate Subscriber to manipulate emitted items. RxJava comes with a huge collection of operators, but its best to focus on just a handful at first.
- map() used to transform one emitted item into another;
Key idea #1: Observable and Subscriber can do anything.
Your Observable could be a database query, the Subscriber taking the results and displaying them on the screen.
Your Observable could be a click on the screen, the Subscriber reacting to it.
Your Observable could be a stream of bytes read from the internet, the Subscriber could write it to the disk.
Key idea #2: The Observable and Subscriber are independent of the transformational steps in between them.
I can stick as many map() calls as I want in between the original source Observable and its ultimate Subscriber. The system is highly composable: it is easy to manipulate the data. As long as the operators work with the correct input/output data I could make a chain that goes on forever
Schedulers
Schedulers determine the thread on which Observables emit their asynchronous data streams and the thread on which Observers consume those data streams.
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
Reactive Extensions
Reactive Extensions(ReactiveX) is as a library for composing asynchronous and event-based programs by using observable sequences. It extends the observer pattern to support sequences of data and/or 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, concurrent data structures, and non-blocking I/O.
Reactive programming
Reactive programming - is focused on reacting to
changes, such as data values or events.
A callback is an approach to reactive programming done imperatively
Subscription
Any call to Observable.subscribe() returns a Subscription. Subscriptions represent a connection between an Observable that’s emitting data and an Observer that’s consuming that data.
Subscriptions give us the ability to part that connection by calling Subscription.unsubscribe().