Welcome to RxJava2
RxJava is a port from Reactive Extensions (Rx) to Java.
RxJava was open sourced 2014 and is hosted at http://reactivex.io/.
ReactiveX
• The Observer pattern done right.
• ReactiveX is a combination of the best ideas from the Observer pattern, the Iterator
pattern, and functional programming.
• To understand different operators from Rx visit http://rxmarbles.com/
Benefits
• It removes complex management of Threads and States.
• It removes “Callback Hell”
• Removes redundant calls to database or webservices with the help of
cache.
• Helps to write Clean and Testable code.
Observer Pattern
• The observer pattern is a software
design pattern in which an object,
called the subject, maintains a list of its
dependents, called observers, and
notifies them automatically of any
state changes, usually by calling one of
their methods.
• It is mainly used to implement
distributed event handling systems.
Type of Observables
• Flowable<T>
• Observable<T>
• Single<T>
• Maybe<T>
• Completable
Observable
Observable<Todo> todoObservable = Observable.create(emitter -> {
try {
List<Todo> todos = getTodos();
for (Todo todo : todos) {
emitter.onNext(todo);
}
emitter.onComplete();
} catch (Exception e) {
emitter.onError(e);
}
});
Maybe
Maybe<List<Todo>> todoMaybe = Maybe.create(emitter -> {
try {
List<Todo> todos = getTodos();
if(todos != null && !todos.isEmpty()) {
emitter.onSuccess(todos);
}else {
emitter.onComplete();
}
} catch (Exception e) {
emitter.onError(e);
}
});
Single<T> and Completable
Single<T> objects are similar to Maybe<T> objects, but only without
the onComplete() method.
Completable objects are pretty similar to Single<T> objects, but
without return value and therefore also do not have a generic type <T>
like the other types. Completable objects can also be seen as reactive
java.lang.Runnable objects.
Observer
DisposableObserver<Todo> disposableObserver = todoObservable.subscribeWith(new DisposableObserver<Todo>() {
@Override
public void onNext(Todo t) {
}
@Override
public void onError(Throwable e) {
}
@Override
public void onComplete() {
}
});
Disposable
Single<List<Todo>> todosSingle = getTodos();
Disposable disposable = todosSingle.subscribeWith(new DisposableSingleObserver<List<Todo>>() {
@Override
public void onSuccess(List<Todo> todos) {
// work with the resulting todos
}
@Override
public void onError(Throwable e) {
// handle the error case
}
});
// continue working and dispose when value of the Single is not interesting any more
disposable.dispose();
Flowable<T> and Backpressure
• RxJava 2.0 introduced a new type Flowable<T>, which is pretty much the
same as an Observable<T> in regards of the API, but Flowable<T> supports
backpressure and Observable<T> does not.
• Back in RxJava 1.0 the concept of backpressure came too late and was
added to Observable<T> types, but some did throw a
MissingBackpressureException, so distinguishing between Flowable<T> and
Observable<T> is a good thing.
• Besides Observable<T> also Maybe<T>, Single<T> and Completable have
no backpressure.
Conversion
From / To Flowable Observable Maybe Single Completable
Flowable toObservable() reduce()
elementAt()
firstElement()
lastElement()
singleElement()
scan()
elementAt()
ignoreElements()
Observable toFlowable() reduce()
elementAt()
firstElement()
lastElement()
singleElement()
scan()
elementAt()
first()/firstOrError()
last()/lastOrError()
single()/singleOrErr
or()
all()/any()/count()
(and more…​)
ignoreElements()
Maybe toFlowable() toObservable() toSingle()
sequenceEqual()
toCompletable()
Single toFlowable() toObservable() toMaybe() toCompletable()
Completable toFlowable() toObservable() toMaybe() toSingle()
toSingleDefault()
Testing the observables
Observable<String> obs = ...// assume creation code here
TestSubscriber<String> testSubscriber = new TestSubscriber<>();
obs.subscribe(testSubscriber);
testSubscriber.assertNoErrors();
List<String> chickens = testSubscriber.getOnNextEvents();
Thank you.

Welcome to rx java2

  • 1.
    Welcome to RxJava2 RxJavais a port from Reactive Extensions (Rx) to Java. RxJava was open sourced 2014 and is hosted at http://reactivex.io/.
  • 2.
    ReactiveX • The Observerpattern done right. • ReactiveX is a combination of the best ideas from the Observer pattern, the Iterator pattern, and functional programming. • To understand different operators from Rx visit http://rxmarbles.com/
  • 3.
    Benefits • It removescomplex management of Threads and States. • It removes “Callback Hell” • Removes redundant calls to database or webservices with the help of cache. • Helps to write Clean and Testable code.
  • 4.
    Observer Pattern • Theobserver pattern is a software design pattern in which an object, called the subject, maintains a list of its dependents, called observers, and notifies them automatically of any state changes, usually by calling one of their methods. • It is mainly used to implement distributed event handling systems.
  • 5.
    Type of Observables •Flowable<T> • Observable<T> • Single<T> • Maybe<T> • Completable
  • 6.
    Observable Observable<Todo> todoObservable =Observable.create(emitter -> { try { List<Todo> todos = getTodos(); for (Todo todo : todos) { emitter.onNext(todo); } emitter.onComplete(); } catch (Exception e) { emitter.onError(e); } });
  • 7.
    Maybe Maybe<List<Todo>> todoMaybe =Maybe.create(emitter -> { try { List<Todo> todos = getTodos(); if(todos != null && !todos.isEmpty()) { emitter.onSuccess(todos); }else { emitter.onComplete(); } } catch (Exception e) { emitter.onError(e); } });
  • 8.
    Single<T> and Completable Single<T>objects are similar to Maybe<T> objects, but only without the onComplete() method. Completable objects are pretty similar to Single<T> objects, but without return value and therefore also do not have a generic type <T> like the other types. Completable objects can also be seen as reactive java.lang.Runnable objects.
  • 9.
    Observer DisposableObserver<Todo> disposableObserver =todoObservable.subscribeWith(new DisposableObserver<Todo>() { @Override public void onNext(Todo t) { } @Override public void onError(Throwable e) { } @Override public void onComplete() { } });
  • 10.
    Disposable Single<List<Todo>> todosSingle =getTodos(); Disposable disposable = todosSingle.subscribeWith(new DisposableSingleObserver<List<Todo>>() { @Override public void onSuccess(List<Todo> todos) { // work with the resulting todos } @Override public void onError(Throwable e) { // handle the error case } }); // continue working and dispose when value of the Single is not interesting any more disposable.dispose();
  • 11.
    Flowable<T> and Backpressure •RxJava 2.0 introduced a new type Flowable<T>, which is pretty much the same as an Observable<T> in regards of the API, but Flowable<T> supports backpressure and Observable<T> does not. • Back in RxJava 1.0 the concept of backpressure came too late and was added to Observable<T> types, but some did throw a MissingBackpressureException, so distinguishing between Flowable<T> and Observable<T> is a good thing. • Besides Observable<T> also Maybe<T>, Single<T> and Completable have no backpressure.
  • 12.
    Conversion From / ToFlowable Observable Maybe Single Completable Flowable toObservable() reduce() elementAt() firstElement() lastElement() singleElement() scan() elementAt() ignoreElements() Observable toFlowable() reduce() elementAt() firstElement() lastElement() singleElement() scan() elementAt() first()/firstOrError() last()/lastOrError() single()/singleOrErr or() all()/any()/count() (and more…​) ignoreElements() Maybe toFlowable() toObservable() toSingle() sequenceEqual() toCompletable() Single toFlowable() toObservable() toMaybe() toCompletable() Completable toFlowable() toObservable() toMaybe() toSingle() toSingleDefault()
  • 13.
    Testing the observables Observable<String>obs = ...// assume creation code here TestSubscriber<String> testSubscriber = new TestSubscriber<>(); obs.subscribe(testSubscriber); testSubscriber.assertNoErrors(); List<String> chickens = testSubscriber.getOnNextEvents();
  • 14.