Slide 1

Slide 1 text

Tango with RxJava

Slide 2

Slide 2 text

Tato Kutalia tatocaster @eMoney.ge https://tatocaster.me github.com/tatocaster twitter.com/@TatoKutalia

Slide 3

Slide 3 text

Problems in app • callback-hell • manage tons of async code • error handling • code maintenance

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

Rx

Slide 6

Slide 6 text

Reactive Extensions

Slide 7

Slide 7 text

RxJava

Slide 8

Slide 8 text

What is RxJava Observables Observers Subscribers Schedulers Operators

Slide 9

Slide 9 text

“RxJava is a library that allows us to represent any operation as an asynchronous data stream that can be created on any thread, declaratively composed, and consumed by multiple objects on any thread.”

Slide 10

Slide 10 text

“RxJava is a library that allows us to represent any operation as an asynchronous data stream that can be created on any thread, declaratively composed, and consumed by multiple objects on any thread.” ?

Slide 11

Slide 11 text

What is reactive • programming with asynchronous data streams. • A stream is a sequence of ongoing events ordered in time.

Slide 12

Slide 12 text

No content

Slide 13

Slide 13 text

Marble Diagram

Slide 14

Slide 14 text

How To Install compile ‘io.reactivex:rxandroid:x.y.z' 
 compile 'io.reactivex:rxjava:x.y.z'

Slide 15

Slide 15 text

Why RxJava • Abstraction of complexity of async data flows and events • Filtering, composition, transformation • Error handling • Light and high performance

Slide 16

Slide 16 text

Observables • This is the data source, the object that will be emitting the sequences of data items that the entities who are interested in the data they provide, will be subscribing. • It implements all the operators mentioned before to be able to transform the data streams. • Observables can be divided in two types: “Hot” observables which might emit data as soon as they are created and “Cold” observables which wait until an Observer subscribes to it.

Slide 17

Slide 17 text

Observers Observers are consumers of an Observable’s asynchronous data stream. Observers can react to the data emitted by the Observable in whatever way they want.

Slide 18

Slide 18 text

Observers

Slide 19

Slide 19 text

Subscribers Subscriber is just an Observer that can, among other things, unsubscribe itself from the items emitted by an Observable.

Slide 20

Slide 20 text

No content

Slide 21

Slide 21 text

Schedulers • Schedulers determine the thread on which Observables emit their asynchronous data streams and the thread on which Observers consume those data streams. • RxJava allows us to create and consume asynchronous data streams on any thread. Observable.create(new Observable.OnSubscribe() { //...
 })
 .subscribeOn(Schedulers.io())
 .observeOn(AndroidSchedulers.mainThread());

Slide 22

Slide 22 text

Operators Creating Transforming Filtering Combining Error Handling Conditional Mathematical full list at http://reactivex.io/documentation/operators.html https://github.com/ReactiveX/RxJava/wiki/Alphabetical-List-of-Observable-Operators

Slide 23

Slide 23 text

Operators Operators allow us to declaratively compose Observables An operator is applied to a “source” Observable and it returns a new Observable as a result

Slide 24

Slide 24 text

Operators Observable.create(new Observable.OnSubscribe() {
 //Emitting story objects...
 })
 .map(new Func1() {
 @Override
 public String call(Story story) {
 return story.getTitle();
 }
 });

Slide 25

Slide 25 text

RxJava in Android

Slide 26

Slide 26 text

RxJava in Android • we don’t control lifecycle • Advantage over AsyncTask, Loader, Handlers

Slide 27

Slide 27 text

Avoiding Memory Leaks Any call to Observable.subscribe() returns a Subscription @Override
 public void onCreate() { //...
 mSubscription = storiesObservable.subscribe(new Observer() {
 @Override
 public void onNext(Story story) {
 Log.d(TAG, story);
 }
 });
 }
 
 @Override
 public void onDestroy() {
 mSubscription.unsubscribe();
 }

Slide 28

Slide 28 text

More for Android RxLifecycle is a project helps Android developers use RxJava within Activity without causing leaks. developed by Trello. https://github.com/trello/RxLifecycle RxBinding by Jake Wharton. Binding API’s for UI widgets. https://github.com/JakeWharton/RxBinding

Slide 29

Slide 29 text

UNLEASH THE BEAST searchViewTextObservable.filter(new Func1() { //1
 @Override
 public Boolean call(String s) {
 return s.length() > 2;
 }
 })
 .debounce(QUERY_UPDATE_DELAY_MILLIS, TimeUnit.MILLISECONDS) //2
 .flatMap(new Func1>>() { //3
 @Override
 public Observable> call(String s) {
 return mAdapter.getSearchStoriesObservable(s);
 }
 }
 )
 .subscribeOn(Schedulers.io())
 .observeOn(AndroidSchedulers.mainThread())
 .subscribe(new Observer>() {
 //...
 @Override
 public void onNext(List stories) {
 mStoriesRecyclerView.setAdapter(new StoryAdapter(stories));
 }
 });

Slide 30

Slide 30 text

UNLEASH THE BEAST searchViewTextObservable.filter(new Func1() { //1
 @Override
 public Boolean call(String s) {
 return s.length() > 2;
 }
 })
 .debounce(QUERY_UPDATE_DELAY_MILLIS, TimeUnit.MILLISECONDS) //2 .mergeWith(historicalQueryTappedObservable)
 .flatMap(new Func1>>() { //3
 @Override
 public Observable> call(String s) {
 return mAdapter.getSearchStoriesObservable(s);
 }
 }
 )
 .subscribeOn(Schedulers.io())
 .observeOn(AndroidSchedulers.mainThread())
 .subscribe(new Observer>() {
 //...
 @Override
 public void onNext(List stories) {
 mStoriesRecyclerView.setAdapter(new StoryAdapter(stories));
 }
 });

Slide 31

Slide 31 text

Convert Async to Rx Observable.create(new Observable.OnSubscribe() {
 @Override
 public void call(Subscriber subscriber) {
 // Synchronous code written in AsyncTask’s doInBackground()
 }
 })
 .subscribeOn(Schedulers.newThread())
 .observeOn(AndroidSchedulers.mainThread())
 .subscribe();

Slide 32

Slide 32 text

Testing RxJava • comes with TestSubscriber class + jUnit • test Subscription , Observables

Slide 33

Slide 33 text

See More https://github.com/tatocaster/TangoWithRxJava Repo contains a very dumb and simple demonstration of operators and subscriptions (CompositeSubscription) and sample Unit Test

Slide 34

Slide 34 text

People you should follow • David Karnok (project lead of RxJava) • Ivan Morgilo (Author of RxJava essentials) • Jake Wharton (Square) • Dan Lew (Trello) • Artem Zinnatulin

Slide 35

Slide 35 text

Observable.just(“Questions?”)subscribe(new Observer() { @Override
 public void onNext(Object o) {
 Log.d(TAG, "onNext() called with: o = [" + o + "]");
 }
 @Override
 public void onCompleted() {
 Log.d(TAG, "Thanks");
 }
 @Override
 public void onError(Throwable e) {
 // ignore this shit
 }
 
 }); );