RxJava Flashcards

1
Q

What’s RxJava?

A

RxJava is a reactive programming library for composing asynchronous and event-based programs by using observable sequences.

Reactive programming is based on data streams and the propagation of change. With reactive programming, you can express static (such as arrays), or dynamic (such as event emitters) data streams with ease.

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

Where can you use RxJava?

A

There is a multitude of places you can use RxJava, and below are the most common places where you can implement it:

  • Network Calls (such as API calls through HTTP with Retrofit, which fully supports RxJava);
  • UI events that should trigger actions;
  • Database Read and Write and/or files in the system;
  • Data coming out of the sensors;
  • Etc
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What are the main elements of RxJava?

A
  • Observable
  • Operator
  • Observer
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Describe Observable

A

An Observable is where the data stream comes from, it does some work and emits values.

You can think about that like it was a Speaker

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

Describe Operator

A

An Operator has the capability to modify the data from one form to another.

You can think about that like it was a Translator

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

Describe Observer

A

An Observer receives the values.

You can think about that like it was a Listener

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

What does it need to use RxJava in Android project?

A

You need to add dependencies in app level Build.gradle:

implementation “io.reactivex.rxjava2:rxjava:2.2.7”
implementation “io.reactivex.rxjava2:rxandroid:2.1.1”

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

Diffrent ways of creating Observable

A
  • just - The just operator converts an Item into an Observable and emits it
  • from* - For example fromArray, fromIterable
  • create - This way you can create an Observable from the ground up
  • interval - This function will create an infinite sequence of ticks, separated by the specified duration.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is Backpressure?

A

Backpressure is the process of handling an emitter that will produce a lot of items very fast. Let’s say an Observable produces 100000 items per second, how will a subscriber that can only handle 100 items per second process those items?
The Observable class has an unbounded buffer size, it buffers everything and pushed onto the subscriber, and if it’s emitting more than it can handle, you’re bound to get OutOfMemoryException .
We can handle such a stream of data if we apply Backpressure to the items as needed, in this way it the unnecessary items will be discarded or even let the producer know when to create or when to push the newly emitted item.

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

How can you solve Backpressure problem?

A

The solution is easy. Instead of an Observable you can use a Flowable which will handle the Backpressure for you since it takes it into consideration whereas Observable do not.

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

Emitter types in RxJava

A
  • Observable
  • Flowable
  • Maybe
  • Single
  • Completable
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Flowable emitter type

A

It works exactly like an Observable but it supports Backpressure.

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

Maybe emitter type

A

This class is used when you’d like to return a single optional value. The methods are mutually exclusive, in other words, only one of them is called. If there is an emitted value, it calls onSuccess , if there’s no value, it calls onComplete or if there’s an error, it calls onError .

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

Single emitter type

A

It’s used when there’s a single value to be returned. If we use this class and there is a value emitted, onSuccess will be called. If there’s no value, onError will be called.

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

Completable emitter type

A

A completable won’t emit any data, what it does is let you know whether the operation was successfully completed. If it was, it calls onComplete and if it wasn’t it calls onError . A common use case of completable is for REST APIs, where successful access will return HTTP 204 , and errors can ranger from HTTP 301 , HTTP 404 , HTTP 500 , etc. We might do something with the information.

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

Can you manually call the methods doOnSubscribe, doOnNext, etc?

A

You can also manually call the methods

doOnSubscribe, doOnNext, doOnError, doOnComplete.
Observable.just(“Hello”)
.doOnSubscribe { println(“Subscribed”) }
.doOnNext { s -> println(“Received: $s”) }
.doAfterNext { println(“After Receiving”) }
.doOnError { e -> println(“Error: $e”) }
.doOnComplete { println(“Complete”) }
.doFinally { println(“Do Finally!”) }
.doOnDispose { println(“Do on Dispose!”) }
.subscribe { println(“Subscribe”) }

17
Q

Is RxJava by default multithreaded?

A

Although RxJava is heavily marketed as an asynchronous way of doing reactive programming, it’s important to clarify that RxJava is single threaded by default, and you need to specify otherwise, and that’s where Schedulers come in.

18
Q

Define Synchronous programming

A

With Synchronous programming, only one thing happens at a time. The code fires up method a, which Reads/Write from the database, and waits for a to finish before moving on to b. So you get one thing happening at a time, and it’s the most common cause for UI freeze since the code will also run in the same thread as the UI.

19
Q

Define Asynchronous programming

A

With Asynchronous programming, you can call many methods at once, without waiting for another to finish. It’s one of the most fundamentals aspects of Android Development, you do not want to run every code on the same thread as the UI, especially computational code.

20
Q

SubscribeOn method, what it does?

A
With subscribeOn you get to decide which thread your Emitter (such as Observable , Flowable , Single , etc) is executed.
The subscribeOn (as well as the observeOn ) needs the Scheduler param to know which thread to run on.
21
Q

What is the most common type of Scheduler?

A

Scheduler.io() This is the most common types of Scheduler that are used. They’re generally used for IO related stuff, such as network requests, file system operations, and it’s backed by a thread pool. A Java Thread Pool represents a group of worker threads that are waiting for the job and reuse many times.

22
Q

Types of Schedulers

A
  • Scheduler.io()
  • Scheduler.computation()
  • Scheduler.newThread()
  • Scheduler.single()
  • Scheduler.trampoline()
  • Executor Scheduler
  • AndroidSchedulers.mainThread()
23
Q

observeOn method, define how it work

A

The method subscribeOn() will instruct the source Observable which thread to emit the items on and push the emissions on our Observer . But if it finds an observeOn() in the chain, it switches the emissions using the selected scheduler for the remaining operation.

24
Q

Transformer in RxJava, how it works

A

With a transformer, we can avoid repeating some code by applying the most commonly used chains among your Observable. For example:

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

Observable.just("Apple", "Orange", "Banana")
    .compose(applyObservableAsync())
    .subscribe { v -> println("The First Observable Received: $v") }

Observable.just("Water", "Fire", "Wood")
    .compose(applyObservableAsync())
    .subscribe { v -> println("The Second Observable Received: $v") }

}

fun applyObservableAsync(): ObservableTransformer {
return ObservableTransformer { observable ->
observable
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
}
}

25
Q

Types of Transformers

A
ObservableTransformer
FlowableTransformer
SingleTransformer
MaybeTransformer
CompletableTransformer
26
Q

Most common operators in RxJava

A

There are many operators that you can add on the Observable chain, most common:

  • map()
    Transforms values emitted by an Observable stream into a single value.
  • flatMap()
    Unlike the map() operator, the flatMap() will transform each value in an Observable stream into another Observable , which are then merged into the output Observable after processing.
- zip() 
The zip() operator will combine the values of multiple Observable together through a specific function.
  • concat()
    As the name suggests, concat() will concatenate (join together) two or more Observable .

-merge()
merge() works similarly to concat() , except merge will intercalate the emissions from both Observable , whereas concat() will wait for one to finish to show another.

  • filter()
    Filter the values according to a set condition.
  • repeat()
    This operator will repeat the emission of the values however many times we may need.
-take()
The take() operator is meant to grab however many emissions you’d like.
27
Q

What is Disposable?

A

A Disposable will release memory, resources, and threads used by an Observable . So, the main purpose of disposable is to free up system resources and make your app more stable.

Remember when dealing with dispose() you should use it when you know for certain that your thread has finished. The most elegant way of doing it is disposing of your Observable through the activity lifecycle, especially onDestroy .

28
Q

What is PublishSubject?

A

PublishSubject works like an Observable and an Observer at the same time.

A quick refresh on the difference between those two.
Observer — Any object that wants to be notified when another object changes.
Observable — Any object whose state may be of interest, and in whom another object may register an interest.
So, a PublishSubject will receive the values as an Observer would and also emit it as an Observable would.