Sunteți pe pagina 1din 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.

S-ar putea să vă placă și