Skip to content

Instantly share code, notes, and snippets.

@EbenezerGH
Created February 8, 2018 05:48
Show Gist options
  • Save EbenezerGH/a52ff2181c22ee8f4f340fca3ecb7ef8 to your computer and use it in GitHub Desktop.
Save EbenezerGH/a52ff2181c22ee8f4f340fca3ecb7ef8 to your computer and use it in GitHub Desktop.
Chapter 1: Que es RxJava?
RxJava is a Java VM implementation of Reactive Extensions
Reactive Extensions: a library for composing asynchronous and event-based programes by using observables
Observable: Emits the value
Observer: Retrieves the value by subscribing to observable
Flowable, Observable, Single, Completable, Maybe: Does work and emit value
Subscription: Work is going on or completed or is used to cancel
Operators: Modify data
Schedulers: Where the work should be done, which thread
Subscriber/Disposable: Where the response will be sent after work has been completed
Example:
getObservable()
.subscribeOn(Schedulers.io()) // run on a background thread
.observeOn(AndroidSchedulers.mainThread()) // be notified on the main thread
.subscribe(getObserver());
Types of Observables (5 types)?
Observable: Emits o..N elements, and then completes successfully or with an error
Flowable: Similar to Observable, but with backpressure strategy
Single: It complets with a value successfully or an error
Maybe: It completes with/without a value or completes with an error
Completeable: It just signals if it has completed successfully or with an error
Schedulers?
SubscribeOn: Specify which scheduler invokes the code contained in Observable.create()
ObserveOn: Allows control to which Scheduler executes the code in the downstream operators
Schedulers.computation(): Used for CPU intensive tasks
Schedulers.io(): Used for IO bound tasks
Schedulers.from(Executor): Use with custom ExecutorService
Schedulers.newThread(): Always creates a new thread whe na worker is needed. Since it's not thread
pooled and always creates a new thread instead of reusing one, this scheduler is not very useful
Chapter 2: Operators?
Operators are basically a set of functions that can operate on any observable and defines the observable, how and when
it should emit the data stream
Map: transforms the items emitted by an Observable by applying a function to each item
Zip: combines the emissions of multiple observables together via a specified function, then emits a single item for each
combination based on the results of the function
Filter: emits only those items from an observable that pass a predicate test
FlatMap: it transforms the items emitted by an Observable into Observables, then flattens the emissions from those into a
single Observable
Take: emits only the first n items emitted by an Observable
Reduce: applies a function to each item emitted by an Observable, sequentially, and emits a final value
Skip: suprresses the first n items emitted by an Observable
Buffer: periodically gathers items emitted by an Observable into bundles and emits these bundles ratehr than emitting the
items one at a time
Concat: emits the emissions from two or more Observables without interleaving them.
Replay: ensures that all observers see the same sequence of emitted items, even if they subscribe after the Observable has
begun emitting items.
Merge: combines multiple Observables into one by merging their emissions
Chapter 3: Rx Advance
Subject: sort of bridge or proxy that is available in some implementations of ReactiveX that acs as an observer and as an Observable
Types of Subjects:
Publish Subject - gets only from when you started observing and onward
Replay Subject - gets everything no matter when you started observing
Behavior Subject - gets the most recently emitted and onward for context
Async Subject - gets only the last observed value
Disposables:
A way to unsubscribe to avoid memory leaks
clear(): will clear all, can accept new disposable
dispose: will clear all and set isDisposed = true so it will not accept any new disposable
Backpressure:
In cases where a publisher is emitting items mroe rapidly than an operator or subscriber can
consume them, then the items that are overflowing from the publisher need to be handled. Backpressure
is a way to avoid OutofMemoryError by signaling how much data it can consume
BackpressureStrategy.Buffer
"".Drop
"".Latest
"".Error
"".Missing
@EbenezerGH
Copy link
Author

public class RxJavaUnitTest {
String result="";

// Simple subscription to a fix value
@Test
public void returnAValue(){
    result = "";
    Observable<String> observer = Observable.just("Hello"); // provides datea
    observer.subscribe(s -> result=s); // Callable as subscriber
    assertTrue(result.equals("Hello"));
}

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment