Please enable JavaScript.
Coggle requires JavaScript to display documents.
ReactiveX Java for android :yellow_heart: (Creating Observable (Observable…
ReactiveX Java for android :yellow_heart:
Main Concept
Observables
Observables emit asynchronous data streams
Fetches the items of an asynchronous data stream and
notifies a Subscriber that those items have become available
Observers
The next component to the Observable stream is the Observer (or Observers) subscribed to it. Observers are notified whenever something happens in the stream.
Observer#onNext(T) - invoked when an item is emitted from the stream
Observable#onError(Throwable) - invoked when an error has occurred within the stream
Observable#onCompleted() - invoked when the stream is finished emitting items.
define
Is a library that allows us to represent and operation
as an asynchronous data stream that can be created
on any thread, declarative composed and consumed by multiple objects on any thread
Observer pattern
Creating Observable
Observable.create()
advanced use only! create an Observable from scratch by means of a function, consider fromEmitter instead
Observalble.just()
Convert an object or several objects into an Observable that emits that object or those objects
Observable.from()
convert an Iterable, a Future, or an Array into an Observable
fromEmitter()
create safe, backpressure-enabled, unsubscription-supporting Observable via a function and push events.
defer()
do not create the Observable until a Subscriber subscribes; create a fresh Observable on each subscription (waiting until the last minute (that is, until subscription time) to generate the Observable can ensure that this Observable contains the freshest data)
range()
create an Observable that emits a range of sequential integers
interval()
create an Observable that emits a sequence of integers spaced by a given time interval
timer()
create an Observable that emits a single item after a given delay
empty()
create an Observable that emits nothing and then completes
nothing()
create an Observable that emits nothing and then signals an error
never()
create an Observable that emits nothing at all
Observable and Operators
Filtering Observables
Selectively emit items from a source observable
(source [2, 30, 22, 5, 60 ,1 ] => filter(x => x> 10) => emits object [30, 22, 60])
The functions
Debounce
Only emit an item from an Observable if a particular time span has passed without it emitting another item
Distinct
Suppress duplicate items emitted by an Observable
Filter
Emit only those items from an Observable that pass a predicate test
ElementAt
emit only item n emitted by an Observable
First
emit only the first item , or the first item that meets a condition, from an Observable
IgnoreElements
do not emit any items from an Observable but mirror its termination notificaton
Last
emit only the last item emitted by an Observable
Sample
emit the most recent item emitted by an Observable within periodic time intervals
Skip
Suppress the first n items emitted by an Observable
SkipLast
Suppress the last n items emitted by an Observable
Take
Emit only the first n items emitted by an Observable
Take Last
Emit only the last n items emitted by an Observable
Transform Observables
first items [1 ,2 ,3] => map(x => 10 * x ) => item emitted [10, 20, 30]
The functions
Buffer
Periodically gather items form an Observable into bundles and emit these bundles rather emitting the items one at a time
FlatMap
transform the items emitted by an Observable into Observables, then flatten the emissions from those into a single Observable
GroupBy
Divide an Observable into a set of Observables that each emit a different group of items from the original observable, organized by key
Map
Transform the items emitted by an Observable by applying a function to each item
Scan
Apply a function to each item emitted by an Observable, sequentially, and emit each successive value
Window
periodically subdivide items from an Observable into Observable windows and emit these windows rather than emitting the items one at a time
ReactiveX2
No longer accepts null value
Throw a NullPointerException
Flowable
like Observable
but is handle follow backpressure
Using when
cold and non timed sources
generator like sources
network and database accessors
Backpressure
If when we subscribe an observable and the Observable is emitting items more rapidly than an operator or subscriber can consume them, so what to do with such a growing backlog of unconsumed items -> handle backpressure
Type
Hot Observable
Cold Observable
Multicasted
Operator that avoid the need for backpressure
sample
throttleFirst
debounce
Observable Utility Operators
ObserveOn()
Specify the scheduler on which and observer will observe this Observable
SubscriberOn()
Instructs the Observable to it self on the specified Scheduler
Delay()
Make slowly the emission from an Observable in time by a particular amount
do..
register an action to take upon a variety of observable lifecycle events
materialize()
convert an Observable into a list of Notifications
dematerialize()
convert a materialized Observable back into its non-materialized form
TimeInterval()
Control the time to emit each item by Observable
Timeout()
If in the timeout not have any emitted items -> abort an observable with an onError
Timestamp()
attach a timestamp to each item emitted by an Observable
Single
rx.Single is a beta feature in RxJava that represents a single value, rather than a stream of values that an rx.Observable represents. This is useful in simplifying the mental model required for consumers of your API (e.g. a Retrofit service).
Subject
Is a proxy between Observable and Subscriber
It can subscribe multiple observables
Emit items as an observable
AsyncSubject
BehaviourSubject
PublishSubject
Replay Subject