Upgrade to Pro — share decks privately, control downloads, hide ads and more …

what's all this fuss about RxJava

what's all this fuss about RxJava

Ayokunle Paul demystifies the concept of reactive programming by diving deep into RxJava and explaining its use cases

Android Nigeria Community

February 19, 2018
Tweet

More Decks by Android Nigeria Community

Other Decks in Programming

Transcript

  1. • Reactivity revolves about the concept connecting different parts of

    the application to each other. • Such that one component can correctly react to the output of its counterpart if need be. • This way, users can see real time response of their actions. • In a nutshell, an app is said to be reactive if it responds to users action(s) in real time.
  2. • Reactive eXtension is the combination of the best ideas

    from the Observer pattern, Iterator pattern and functional programming. http://reactivex.io/ • RxJava - ReactiveX for the JVM - A library for composing asynchronous and event-based programs using observable sequences for the JVM https://github.com/ReactiveX/RxJava • Observer Pattern - A software design principle in which an Object, called the subject, maintains a list of dependents, called Observers, and notify them automatically of any state changes, usually by calling one of their methods. https://en.wikipedia.org/wiki/Observer_pattern
  3. Observable • Set of classes that can continuously emit stream

    of data. • Data emitted can range from 0 to infinity • They may or may not emit any data • Synchronously or asynchronously • Starts when you subscribe or unsubscribe • Stops either due to an error or completion
  4. Observable Hot Cold Observables that start emitting data upon creation.

    Observables that start emitting data upon subscription.
  5. Observer • Set of classes (or method definition) that can

    asynchronously react to emissions by Observables • They do so by “subscribing” to the observable • Then override the onNext(...), onError(...), onCompleted(...) methods • onNext() is called whenever the Observable emits data • onCompleted() is called whenever the Observable stops emitting data • onError() is called if an error occurs • onCompleted() and onError() are terminal methods • To avoid memory leaks, ensure you unsubscribe after a complete transaction • This is best done in the onStop() lifecycle callback.
  6. Subscribing and Unsubscribing Observable<String> observable = Observable.just(“1”, “2”, “3”, “4”);

    DisposableObserver observer = new DisposableObserver<String>{ @Override public void onNext(String s){ … } @Override public void onComplete(){ … } @Override public void onError(Throwable t){ … } } observable.subscribe(observer); //unsubscribe to avoid possible resource leak @Override public void onStrop(){ observer.dispose(); }
  7. Subscribing and Unsubscribing Observable<String> observable = Observable.just(“1”, “2”, “3”, “4”);

    Disposable disposable = observable.subscribeWith(new DisposableObserver<String>{ @Override public void onNext(String s){ … } @Override public void onComplete(){ … } @Override public void onError(Throwable t){ … } } ); //unsubscribe to avoid possible resource leak @Override public void onStrop(){ disposable.dispose(); }
  8. Subscribing and Unsubscribing Observable<String> observable = Observable.just(“1”, “2”, “3”, “4”);

    Disposable disposable = observable.subscribeWith(new DisposableObserver<String>{ @Override public void onNext(String s){ … } @Override public void onComplete(){ … } @Override public void onError(Throwable t){ … } }); CompositeDisposable compositeDisposable = new CompositeDisposable(); compositeDisposable.add(disposable); //unsubscribe to avoid possible resource leak @Override public void onStrop(){ compositeDisposable.dispose(); }
  9. Operators • Set of methods for manipulating observables • Act

    on an Observable to produce another observable. • Manipulate how data are emitted • Manipulate how Observables are observed (threading) • Combine data streamed in different ways
  10. //The normal Java way String mString = “Hello, Android”; String

    mStringUpper = mString.toUppercase(); //The RxJava way Observable<String> mString = Observable.just(“Hello, Android”); Observable<String> mStringUpper = mString.map(v -> v.toUppercase()); Manipulating emissions
  11. //The RxJava way Observable<Location> location = data.getLocation(); Observable<Location> mLocation =

    location.subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()); Manipulating threads
  12. //Order is important Location mLocation; Observable<Location> location = Observable.fromCallable(() ->

    { return mLocation.getLocation(); }) .subscribeOn(Schedulers.io()) .map(location -> location.toString()) .observeOn(AndroidSchedulers.mainThread()); Manipulating threads
  13. Continue exploring… • Specified types of Observables (Single, Maybe, Completable)

    • Other operators (flatMap, groupBy, first, debounce, …) • RxAndroid, RxBinding • Online tutorials (Jake Wharton, Annyce Davice, …)