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

Introduction to RxJava (Android Istanbul Meetup...

Introduction to RxJava (Android Istanbul Meetup, 2016)

alpercempolat

April 25, 2016
Tweet

More Decks by alpercempolat

Other Decks in Programming

Transcript

  1. Reactive Programming • Reactive programming is programming with asynchronous data

    streams. • On top of that, there is an amazing toolbox of functions to combine, create and filter any of those streams.
  2. Why RxJava? • Easy threading • Set of powerful operators

    • Explicit Error Handling • Easy implementation of data flows • Chain method calls • Reduce boilerplate code
  3. Hello World //Create an observable that emits "Hello World" and

    completes Observable<String> myObservable = Observable.create(new Observable.OnSubscribe<String>() { @Override public void call(Subscriber<? super String> subscriber) { subscriber.onNext("Hello world!"); subscriber.onCompleted(); } });
  4. Hello World //Create a subscriber that prints each String emitted

    by the Observable. Subscriber<String> mySubscriber = new Subscriber<String>() { @Override public void onCompleted() { } @Override public void onError(Throwable e) { } @Override public void onNext(String s) { System.out.println(s); } };
  5. Hello World //Create a subscriber that prints each String emitted

    by the Observable. Subscriber<String> mySubscriber = new Subscriber<String>() { @Override public void onCompleted() { } @Override public void onError(Throwable e) { } @Override public void onNext(String s) { System.out.println(s); } }; //Hook them each other with subscribe. myObservable.subscribe(mySubscriber);
  6. Hello World - Simpler Observable<String> justObservable = Observable.just("Hello World!"); Action1<String>

    onNextAction = new Action1<String>() { @Override public void call(String s) { System.out.println(s); } };
  7. Hello World - Simpler Observable<String> justObservable = Observable.just("Hello World!"); Action1<String>

    onNextAction = new Action1<String>() { @Override public void call(String s) { System.out.println(s); } }; justObservable.subscribe(onNextAction);
  8. Hello World - Simpler Observable<String> justObservable = Observable.just("Hello World!"); Action1<String>

    onNextAction = new Action1<String>() { @Override public void call(String s) { System.out.println(s); } }; justObservable.subscribe(onNextAction); Observable.just("Hello World!") .subscribe(new Action1<String>() { @Override public void call(String s) { System.out.println(s); } });
  9. Hello World - Simpler Observable<String> justObservable = Observable.just("Hello World!"); Action1<String>

    onNextAction = new Action1<String>() { @Override public void call(String s) { System.out.println(s); } }; justObservable.subscribe(onNextAction); Observable.just("Hello World!") .subscribe(new Action1<String>() { @Override public void call(String s) { System.out.println(s); } }); Observable.just("Hello World!") .subscribe(s -> System.out.println(s));
  10. Map Operator Observable.just("Hello, world!") .map(new Func1<String, String>() { @Override public

    String call(String s) { return s + " - Alper"; } }) .subscribe(s -> System.out.println(s));
  11. Map Operator Observable.just("Hello, world!") .map(new Func1<String, String>() { @Override public

    String call(String s) { return s + " - Alper"; } }) .subscribe(s -> System.out.println(s)); Observable.just("Hello, world!") .map(s -> s + " - Alper") .subscribe(s -> System.out.println(s));
  12. Map Operator Observable.just("Hello, world!") .map(new Func1<String, Integer>() { @Override public

    Integer call(String s) { return s.length(); } }) .subscribe(i -> System.out.println(Integer.toString(i)));
  13. Map Operator Observable.just("Hello, world!") .map(new Func1<String, Integer>() { @Override public

    Integer call(String s) { return s.length(); } }) .subscribe(i -> System.out.println(Integer.toString(i))); Observable.just("Hello, world!") .map(s -> s.length()) .subscribe(i -> System.out.println(Integer.toString(i)));
  14. Map Operator Observable.just("Hello, world!") .map(new Func1<String, Integer>() { @Override public

    Integer call(String s) { return s.length(); } }) .subscribe(i -> System.out.println(Integer.toString(i))); Observable.just("Hello, world!") .map(s -> s.length()) .subscribe(i -> System.out.println(Integer.toString(i))); Observable.just("Hello, world!") .map(s -> s.length()) .map(i -> Integer.toString(i)) .subscribe(s -> System.out.println(s));
  15. More Operators //Returns list of contributers of a Github repo

    Observable<List<String>> getContributers(String repo);
  16. From Operator List<String> list = Arrays.asList("contributer1", "contributer2", "contributer3"); Observable.from(list) .subscribe(contributers

    -> System.out.println(contributers)); getContributers("rxjava").subscribe(contributers -> { Observable.from(contributers) .subscribe(contributer -> System.out.println(contributer)); });
  17. FlatMap Operator getContributers("rxjava") .flatMap(new Func1<List<String>, Observable<String>>() { @Override public Observable<String>

    call(List<String> contributers) { return Observable.from(contributers); } }) .subscribe(contributer -> System.out.println(contributer));
  18. FlatMap Operator getContributers("rxjava") .flatMap(new Func1<List<String>, Observable<String>>() { @Override public Observable<String>

    call(List<String> contributers) { return Observable.from(contributers); } }) .subscribe(contributer -> System.out.println(contributer)); getContributers("rxjava") .flatMap(contributers -> Observable.from(contributers)) .subscribe(contributer -> System.out.println(contributer));
  19. FlatMap Operator //Returns number of commits of a given user

    Observable<Integer> getCommitCount(String user);
  20. FlatMap Operator //Returns number of commits of a given user

    Observable<Integer> getCommitCount(String user); getContributers("rxjava") .flatMap(contributers -> Observable.from(contributers)) .flatMap(new Func1<String, Observable<Integer>>() { @Override public Observable<Integer> call(String user) { return getCommitCount(user); } }) .subscribe(commitCount -> System.out.println(commitCount));
  21. FlatMap Operator //Returns number of commits of a given user

    Observable<Integer> getCommitCount(String user); getContributers("rxjava") .flatMap(contributers -> Observable.from(contributers)) .flatMap(new Func1<String, Observable<Integer>>() { @Override public Observable<Integer> call(String user) { return getCommitCount(user); } }) .subscribe(commitCount -> System.out.println(commitCount)); getContributers("rxjava") .flatMap(contributers -> Observable.from(contributers)) .flatMap(user -> getCommitCount(user)) .subscribe(commitCount -> System.out.println(commitCount));
  22. Filter Operator getContributers("rxjava") .flatMap(contributers -> Observable.from(contributers)) .flatMap(user -> getCommitCount(user)) .filter(new

    Func1<Integer, Boolean>() { @Override public Boolean call(Integer commitCount) { return commitCount > 0; } }) .subscribe(commitCount -> System.out.println(commitCount));
  23. Filter Operator getContributers("rxjava") .flatMap(contributers -> Observable.from(contributers)) .flatMap(user -> getCommitCount(user)) .filter(new

    Func1<Integer, Boolean>() { @Override public Boolean call(Integer commitCount) { return commitCount > 0; } }) .subscribe(commitCount -> System.out.println(commitCount)); getContributers("rxjava") .flatMap(contributers -> Observable.from(contributers)) .flatMap(user -> getCommitCount(user)) .filter(commitCount -> commitCount > 0) .subscribe(commitCount -> System.out.println(commitCount));
  24. doOnNext Operator getContributers("rxjava") .flatMap(contributers -> Observable.from(contributers)) .flatMap(user -> getCommitCount(user)) .filter(commitCount

    -> commitCount > 0) .take(5) .doOnNext(commitCount -> saveToDisk(commitCount)) .subscribe(commitCount -> System.out.println(commitCount));
  25. Threading Output Filter1: pool-1-thread-3 Map1: pool-1-thread-3 Filter2: pool-1-thread-4 OnCompleted: pool-1-thread-4

    Map2: RxCachedThreadScheduler-1 onNext: RxCachedThreadScheduler-1 onComplete: RxCachedThreadScheduler-1 Observable.just("Hello") .filter(s1 -> true) .subscribeOn(Schedulers.newThread()) .map(s -> s.length()) .subscribeOn(Schedulers.io()) .observeOn(Schedulers.newThread()) .doOnCompleted(() -> { System.out.printf("OnCompleted: %s", currentThread().getName()); }) .filter(s1 -> true) .observeOn(Schedulers.io()) .map(length -> String.valueOf(length)) .subscribe(length -> { System.out.printf("onNext: %s", currentThread().getName()); }, throwable -> { System.out.printf("onError: %s", currentThread().getName()); }, () -> { System.out.printf("onComplete: %s", currentThread().getName()); });
  26. Subscription Subscription subscription = Observable.just("Hello, World!") .subscribe(s -> System.out.println(s)); //Breaks

    the chain subscription.unsubscribe(); //Returns true System.out.println(subscription.isUnsubscribed());
  27. Error Handling Observable.just("Hello, world!") .map(s -> methodThatCanThrowException(s)) .map(s -> otherMethodThatCanThrowException(s))

    .subscribe(new Subscriber<String>() { @Override public void onNext(String s) { System.out.println(s); } @Override public void onCompleted() { System.out.println("Completed!"); } @Override public void onError(Throwable e) { System.out.println(e.getMessage()); } });
  28. Error Handling - Fallback Observable<BaseAccount> observable = getAccount(accountId); observable.onErrorResumeNext(t ->

    { return Observable.just(BaseAccount.DEFAULT); }) .subscribe(p -> System.out.println(p));
  29. Error Handling - Retry Observable<BaseAccount> observable = getAccount(accountId); observable.retry((retryCount, throwable)

    -> { System.out.println("Error occurred when retrieving account" + throwable.getMessage()); return retryCount < 2 ? Boolean.TRUE : Boolean.FALSE; }) .subscribe(s -> System.out.println(s));