0% found this document useful (0 votes)
493 views

ZT Rxjava Cheat Sheet

RxJava provides classes like Observable and Single to emit data asynchronously. Observables can be created from values, collections, or callables. Data can be transformed using functions like map, filter, and flatMap. Observables can be subscribed to using observers to receive emitted items or errors. RxBinding allows turning Android UI events into observables. RxAndroid controls which threads observables observe and emit on to avoid blocking the main thread. Tests can use TestSubscriber to assert observable behavior.

Uploaded by

Didik Ismawanto
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
493 views

ZT Rxjava Cheat Sheet

RxJava provides classes like Observable and Single to emit data asynchronously. Observables can be created from values, collections, or callables. Data can be transformed using functions like map, filter, and flatMap. Observables can be subscribed to using observers to receive emitted items or errors. RxBinding allows turning Android UI events into observables. RxAndroid controls which threads observables observe and emit on to avoid blocking the main thread. Tests can use TestSubscriber to assert observable behavior.

Uploaded by

Didik Ismawanto
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

For m ore

awesom
RxJava cheat sheet vis it rebe e cheat s heets
ll abs.org!

Basic RxJava classes Data processing functions Subscribing to observables


Observable<T> - emits 0 or n items and terminates with map(Function<? super T,? extends R> mapper) - Observers provide a mechanism for receiving data and
complete or an error. applies a function to each of items, and emits the notifications from Observables using the following API:
returned values.
Single<T> - emits either a single item or an error. onNext(T t) - provides the Observer with a new item
The reactive version of a method call. You subscribe to filter(Predicate<? super T> predicate) - to observe.
a Single and you get either a return value or an error. emits only the items satisfying a predicate.
onError(Throwable e) - notifies the Observer that the
Maybe<T> - succeeds with either an item, no item, or errors. buffer(int count) - emits lists of the items of the Observable has experienced an error condition.
The reactive version of an Optional. specified size.
onComplete()- notifies the Observer that the Observable
Completable - either completes or returns an error. zip(ObservableSource s1, ObservableSource s2, has finished sending push-based notifications.
It never return items. The reactive version of a Runnable. BiFunction<T1, T2, R> f) -
applies a function to the items from multiple observables RxLifecycle
Creating observables and emits the returned value.
Bind subscription lifecycle to Android components.
Create an observable from a value, a collection or iterable, Destroy subscriptions and avoid memory leaks on
or a result of a callable: destroy / pause events.
Observable.just("RebelLabs"); myObservable.compose(
Observable.fromIterable(iterable); flatMap { } RxLifecycleAndroid.bindActivity(lifecycle))
Observable.fromCallable(callable); .subscribe();

RxBindings Testing observables


Turns Android UI events into RxJava observables: TestSubscriber - a subscriber that records events that
Button button = (Button) you can make assertions upon.
findViewById(R.id.button); flatMap(Function<? super T,? extends
RxView.clicks(button).subscribe(x -> { ObservableSource<? extends R>> mapper) - TestObserver - an Observer that records events that
// do work here takes a function from items to an Observable, you can make assertions upon.
}); emits the items of the resulting Observables TestSubscriber<Integer> ts =
. Flowable.range(1, 5).test();
groupBy(Function<? super T,? extends K> // assert properties
RxAndroid keySelector) - assertThat(
Control on which threads you observe and react to events emits items grouped by a specified key selector function. ts.values()).hasSize(5));
(avoid long computations on the main thread):
Observable.just("RebelLabs") timeout(long timeout, TimeUnit timeUnit) -
.subscribeOn(Schedulers.newThread()) emits items of the original Observable. If the next item
.observeOn(AndroidSchedulers.mainThread()) isn't emitted within the specified timeout,
.subscribe(anObserver); a TimeoutException occurs.

You might also like