Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Intro to RxJava/RxAndroid - GDG Munich Android

1,434 views

Published on

The RX libraries have gained great popularity among developers lately. RX solves asynchronous programming elegantly based on the Observer pattern. This is especially helpful on Android where lots of interactions are asynchronous to the UI. Speaker: Egor Andreevici

Published in: Software
  • Be the first to comment

Intro to RxJava/RxAndroid - GDG Munich Android

  1. 1. INTRO TO RXJAVA & RXANDROID Egor Andreevici
  2. 2. WHAT? HOW? WHY? & A DEMO
  3. 3. WHAT IS RXJAVA?
  4. 4. “RXJAVA IS A JAVA VM IMPLEMENTATION OF REACTIVEX (REACTIVE EXTENSIONS): A LIBRARY FOR COMPOSING ASYNCHRONOUS AND EVENT-BASED PROGRAMS BY USING OBSERVABLE SEQUENCES.” RxJava Wiki WHAT IS RXJAVA?
  5. 5. WHAT IS RXJAVA? OBSERVABLES (AND MARBLE DIAGRAMS)
  6. 6. WHAT IS RXJAVA? PROGRAM STRUCTURE subscription = Observable .create(…) .transform(…) .subscribe(...);
  7. 7. HOW RXJAVA?
  8. 8. HOW RXJAVA? THE HIGH LEVEL PLAN ▸ Create an Observable ▸ Manipulate data using operators ▸ Subscribe and consume ▸ Profit!
  9. 9. HOW RXJAVA? CREATING OBSERVABLES: CREATE() Observable.create(subscriber -> { try { for (int value = 1; value <= 3; value++) { subscriber.onNext(value); } subscriber.onCompleted(); } catch (Throwable t) { subscriber.onError(t); } });
  10. 10. HOW RXJAVA? THE OBSERVER INTERFACE public interface Observer<T> { void onCompleted(); void onError(Throwable e); void onNext(T t); }
  11. 11. HOW RXJAVA? THE NOTIFICATIONS CONTRACT ▸ Zero or more onNext() notifications ▸ Either onCompleted() or onError(), not both! ▸ Nothing more! ▸ May never terminate ▸ May never call onCompleted() ▸ All notifications must be issued serially
  12. 12. HOW RXJAVA? CREATING OBSERVABLES: JUST() Observable.just(1); Observable.just(1, 2); Observable.just(1, 2, 3); . . . Observable.just(1, … , 9);
  13. 13. HOW RXJAVA? CREATING OBSERVABLES: FROM() Observable.from(Arrays.asList(1, 2, 3)); Observable.from(new Integer[]{1, 2, 3});
  14. 14. HOW RXJAVA? CREATING OBSERVABLES: MISC Observable.empty(); Observable.error(t); Observable.never();
  15. 15. HOW RXJAVA? Observable.create(subscriber -> { try { for (int i = 0; i < 10000; i++) { T value = longRunningNetworkRequest(); subscriber.onNext(value); } subscriber.onCompleted(); } catch (Throwable t) { subscriber.onError(t); } }); CAVEATS: HANDLING UNSUBSCRIBING
  16. 16. HOW RXJAVA? SUBSCRIPTION INTERFACE public interface Subscription { void unsubscribe(); boolean isUnsubscribed(); }
  17. 17. HOW RXJAVA? CAVEATS: HANDLING UNSUBSCRIBING Observable.create(subscriber -> { try { for (int i = 0; i < 10000; i++) { if (subscriber.isUnsubscribed()) { return; } T value = longRunningNetworkRequest(); subscriber.onNext(value); } subscriber.onCompleted(); } catch (Throwable t) { subscriber.onError(t); } });
  18. 18. HOW RXJAVA? OPERATORS ▸ Transformational ▸ map() ▸ flatMap() ▸ concatMap() ▸ Filtering ▸ filter() ▸ take() ▸ skip() ▸ Combining ▸ merge() ▸ zip() ▸ combineLatest() AND MANY MORE…
  19. 19. HOW RXJAVA? OPERATORS: FILTER()
  20. 20. HOW RXJAVA? OPERATORS: MAP()
  21. 21. HOW RXJAVA? OPERATORS: FLATMAP()
  22. 22. HOW RXJAVA? OPERATORS: ZIP()
  23. 23. HOW RXJAVA? SUBSCRIBING TO OBSERVABLES .subscribe( value -> renderValue(value), error -> renderError(error), () -> Log.d(LOGTAG, "Done!"));
  24. 24. HOW RXJAVA? MULTITHREADING: OPERATORS Observable<T> observeOn(Scheduler scheduler) {} Observable<T> subscribeOn(Scheduler scheduler) {}
  25. 25. HOW RXJAVA? MULTITHREADING: SCHEDULERS Schedulers.io() Schedulers.computation() Schedulers.newThread() Schedulers.from(Executor)
  26. 26. HOW RXJAVA? TESTING: BLOCKING OBSERVABLES @Test public void testingWithBlockingObservable() { Observable<Integer> intObservable = Observable.just(1, 2, 3); BlockingObservable<Integer> blocking = intObservable.toBlocking(); assertThat(blocking.first()).isEqualTo(1); assertThat(blocking.last()).isEqualTo(3); blocking.getIterator(); blocking.toIterable(); }
  27. 27. HOW RXJAVA? TESTING: TESTSUBSCRIBER @Test public void testingWithTestSubscriber() { Observable<Integer> intObservable = Observable.just(1, 2, 3); TestSubscriber<Integer> testSubscriber = TestSubscriber.create(); intObservable.subscribe(testSubscriber); testSubscriber.assertValues(1, 2, 3); testSubscriber.assertNoErrors(); testSubscriber.assertCompleted(); }
  28. 28. HOW RXJAVA? DEBUGGING: SIDE EFFECTS Observable.just(1, 2, 3) .doOnNext(next -> append("Before filter: " + next)) .filter(value -> value % 2 == 0) .doOnNext(next -> append("After filter: " + next)) .subscribe(...);
  29. 29. RXJAVA ON ANDROID
  30. 30. RXJAVA ON ANDROID RXANDROID ▸ Super tiny ▸ AndroidSchedulers.mainThread() ▸ HandlerScheduler.from(Handler)
  31. 31. RXJAVA ON ANDROID RXBINDING ▸ RxJava binding APIs for Android's UI widgets ▸ RxView.clicks(…), RxView.enabled(…) etc. ▸ RxTextView.textChanges(…)
  32. 32. RXJAVA ON ANDROID RXLIFECYCLE ▸ Lifecycle handling APIs for Android apps using RxJava ▸ .compose(RxLifecycle.bindActivity(lifecycle)) ▸ RxActivity, RxFragment ▸ .compose(bindToLifecycle())
  33. 33. RXJAVA ON ANDROID RETROFIT: THE UGLY @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); GithubApiClient client = createGithubApiClient(); try { List<Repo> repos = client.getRepos().execute().body(); // render repos } catch (IOException e) { // handle } }
  34. 34. RXJAVA ON ANDROID RETROFIT: THE BAD @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); GithubApiClient client = createGithubApiClient(); client.getRepos().enqueue(new Callback<List<Repo>>() { @Override public void onResponse(Call<List<Repo>> call, Response<List<Repo>> response) { // render repos } @Override public void onFailure(Call<List<Repo>> call, Throwable t) { // handle } }); }
  35. 35. RXJAVA ON ANDROID RETROFIT: THE GOOD @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); GithubApiClient client = createGithubApiClient(); subscription = client.getRepos() .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe( repos -> renderRepos(repos), error -> handleError(error)); }
  36. 36. WHY RXJAVA?
  37. 37. WHY RXJAVA? WHY RXJAVA? ▸ Set of powerful operators ▸ Easy threading ▸ Explicit error handling ▸ Testable ▸ Lots of perks specific to Android
  38. 38. DEMO TIME!
  39. 39. THANK YOU! Egor Andreevici blog.egorand.me · @EgorAnd · +EgorAndreevich

×