RxJava Flashcards

1
Q

RxJava

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Observer Design Pattern

A

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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Publish–subscribe pattern

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Main components of RxJava

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Reactive programming

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Other components of RxJava

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Subscriber vs Observer

A

a Subscriber is just an Observer that can unsubscribe from the Observable that emits items.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Types of Observables

A

> > 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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Operators

A

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;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Key idea #1: Observable and Subscriber can do anything.

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Key idea #2: The Observable and Subscriber are independent of the transformational steps in between them.

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Schedulers

A

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())

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Reactive Extensions

A
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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Reactive programming

A

Reactive programming - is focused on reacting to
changes, such as data values or events.
A callback is an approach to reactive programming done imperatively

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Subscription

A

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().

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Avoid memory leaks

Case 1: Observable may live longer than Observer

A

WHEN
An Observable may live longer than its Observer
because it is emitting items on a separate thread.

HOW TO SOLVE
Connection between Observable-Observer is represented by the Subscription object.
Calling Subscription.unsubscribe() clears the Observable’s reference to the Observer.

WHERE
calling unsubscribe() in an Activity’s onDestroy() method will prevent any leaks from occurring.
17
Q

Avoid memory leaks

Case 2: Annonimous or inner Observable.OnSubscribe function object in Activity

A

WHEN
If you create an Observable in your Activity using an anonymous or (nonstatic) inner Observable.OnSubscribe function object, that object
will hold an implicit reference to your Activity, and if the Observable.OnSubscribe function object lives longer than your Activity, then it will prevent the Activity from being garbage collected evenafter it has received a call to onDestroy().
Code running inside of the call() method is running
on an I/O thread. We can easily imagine a case where this code starts to run on an I/O thread, but then the user closes the Activity that wanted to consume the data emitted by this Observable.
In this case, because the Observable.OnSubscribe function object holds an implicit reference to the Activity, the Activity cannot be garbage collected until the code running in the call() method completes.

HOW TO SOLVE
Ensure that the Observable.OnSubscribe object is an instance of a class that does not have an implicit or explicit reference to your Activity.
18
Q

Backpressure

A

Backpressure is what you get when a source Observable is emitting items faster than a Subscriber can consume them

19
Q

RxAndroid

A

RxAndroid is an extension to RxJava built just for Android. that providers a Scheduler for Android’s Main Thread

20
Q

Operators: just() from()

A

.just() operator to convert any object into an Observable.

.from() operator allows you to convert a collection of objects into an observable stream

21
Q

cold annd hot Observable

A
  • A cold Observable begin emitting this sequence when its Observer desires
  • Examples of items emitted by a cold Observable might include the results of a database query, file retrieval, or web request.
  • A hot Observable begins generating items to emit immediately when it is created.
  • Examples of items emitted by a hot Observable might include mouse & keyboard events, system events, or stock prices.