RxJava is very powerful tool which helps Android code to understand and maintain very easily. Using RxJava, application can be created with lesser bug. With RxJava many complex tasks can be accomplished easily and without errors. As RxJava is new for Android developer, they might have questions like Why? How? When? In this talk, audience will have very useful RxJava information with example of implementing in Applications.
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Welcome to rx java2
1. 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/.
2. 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/
3. 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.
4. 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.
5. Type of Observables
• Flowable<T>
• Observable<T>
• Single<T>
• Maybe<T>
• Completable
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.