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.

Rx java in action

716 views

Published on

Presentation slide for Bekraf Developer Day Yogyakarta, 30 July 2016

Published in: Technology
  • Be the first to comment

  • Be the first to like this

Rx java in action

  1. 1. RxJava in Action Pratama Nur Wijaya Android Developer - Kulina
  2. 2. RxJava is a Java VM implementation of ReactiveX (Reactive Extensions): a library for composing asynchronous and event-based programs by using observable sequences. RxJava ?
  3. 3. Why RxJava Avoid callbackhell Easy to compose and transform async process, combining, filtering, etc Error handling It’s awesome
  4. 4. Cons RxJava - Learning Curve
  5. 5. Implementation dependencies { compile 'io.reactivex:rxandroid:1.2.1' compile 'io.reactivex:rxjava:1.1.6' }
  6. 6. Observable Observable<String> myObservable = Observable.create( new Observable.OnSubscribe<String>() { @Override public void call(Subscriber<? super String> subscriber) { try { subscriber.onNext("Hello World"); subscriber.onCompleted(); } catch (Exception e) { subscriber.onError(e); } } });
  7. 7. Subscriber Subscriber<String> mySubscriber = new Subscriber<String>() { @Override public void onCompleted() { Log.d(TAG, "onCompleted: Completed"); } @Override public void onError(Throwable e) { Log.e(TAG, "onError: Error " + e.getLocalizedMessage()); } @Override public void onNext(String s) { Log.d(TAG, "onNext: " + s); } };
  8. 8. Subscriber myObservable.subscribe(mySubscriber); // result : Hello World
  9. 9. Observable (Simplified) Observable.just("Hello World") .subscribe(new Subscriber<String>() { @Override public void onCompleted() { Log.d(TAG, "onCompleted: Completed"); } @Override public void onError(Throwable e) { Log.e(TAG, "onError: "+e.getLocalizedMessage() ); } @Override public void onNext(String s) { Log.d(TAG, "onNext: " + s); } });
  10. 10. More Simple ? Use Retrolambda
  11. 11. Simplified With Lambda buildscript { repositories { mavenCentral() } dependencies { classpath 'me.tatarka:gradle-retrolambda:3.2.5' } } // Required because retrolambda is on maven central repositories { mavenCentral() } apply plugin: 'com.android.application' //or apply plugin: 'java' apply plugin: 'me.tatarka.retrolambda'
  12. 12. Observable (Simplified with Retrolambda) Observable.just("Hello World") .subscribe( s -> Log.d(TAG, "onNext: " + s), throwable -> Log.e(TAG, "onError: " + throwable.getLocalizedMessage()), () -> Log.d(TAG, "onCompleted: Completed"));
  13. 13. RxJava Operator
  14. 14. Observable.just(1, 2, 3, 4, 5) .map(integer -> integer * 2) .subscribe(result -> { Log.d(TAG, "onCreate: result :"+result); }, throwable -> { Log.e(TAG, "onCreate: "+throwable.getLocalizedMessage()); }, () -> { Log.i(TAG, "onCreate: finished"); }); // result : 2,4,6,8,10
  15. 15. Observable.just(1, 2, 3, 4, 5) .filter(integer -> integer % 2 == 0) .map(integer -> integer * 2) .subscribe(result -> { Log.d(TAG, "onCreate: result :" + result); }, throwable -> { Log.e(TAG, "onCreate: " + throwable.getLocalizedMessage()); }, () -> { Log.i(TAG, "onCreate: finished"); }); // result : 4,8
  16. 16. Check http://rxmarbles.com/ for more operator
  17. 17. RxJava + Asynchronous
  18. 18. Async Observable.just(doNetworkOrLongOperation()) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(s -> { Log.d(TAG, "onCreate: " + s); }, throwable -> { Log.e(TAG, "onCreate: " + throwable.getLocalizedMessage()); }, () -> { Log.i(TAG, "onCreate: completed"); });
  19. 19. RxJava + OkHttp
  20. 20. private Observable<String> getSomeString(){ return Observable.create(new Observable.OnSubscribe<String>() { @Override public void call(Subscriber<? super String> subscriber) { try{ OkHttpClient client = new OkHttpClient(); Request request = new Request.Builder().url("SOME URL").build(); Response response = client.newCall(request).execute(); if (response != null) { final String result = response.body().string(); subscriber.onNext(result); subscriber.onCompleted(); } }catch (Exception e){ subscriber.onError(e); } } }); }
  21. 21. RxJava + OkHttp getSomeString().subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(s -> { Log.d(TAG, "onCreate: " + s); }, throwable -> { Log.e(TAG, "onCreate: " + throwable.getLocalizedMessage()); }, () -> { Log.i(TAG, "onCreate: completed"); });
  22. 22. RxJava + Retrofit 2
  23. 23. RxJava + Retrofit 2.x // dependencies compile "com.squareup.retrofit2:adapter-rxjava:$RETROFIT_VERSION" // setup retrofit Retrofit retrofit = new Retrofit.Builder().baseUrl(BuildConfig.SERVER_URL) .addCallAdapterFactory(RxJavaCallAdapterFactory.create()) .client(okHttpClient) .build();
  24. 24. Service @GET("get_post/") Observable<SinglePostResponse> getPost(@Query("id") int id); // handle it on your presenter dataManager.getPost(id, isUpdate) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(post -> { if (post != null) { // do something } }, throwable -> { Log.e(TAG, "getArticleDetail: "+throwable.getLocalizedMessage() ); }, () -> { Log.i(TAG, "getArticleDetail: completed"); });
  25. 25. Be careful of Memory leak
  26. 26. private CompositeSubscription compositeSubscription = new CompositeSubscription(); compositeSubscription.add(getSomeString().subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(s -> { Log.d(TAG, "onCreate: " + s); }, throwable -> { Log.e(TAG, "onCreate: " + throwable.getLocalizedMessage()); }, () -> { Log.i(TAG, "onCreate: completed"); })); @Override protected void onDestroy() { super.onDestroy(); compositeSubscription.unsubscribe(); }
  27. 27. RxBinding Demo https://github.com/pratamawijaya/RxJavaInAction
  28. 28. References http://blog.danlew.net/2014/09/15/grokking-rxjava-part-1/ https://github.com/kaushikgopal/RxJava-Android-Samples https://www.ykode.com/2015/02/20/android-frp-rxjava- retrolambda.html https://gist.github.com/staltz/868e7e9bc2a7b8c1f754
  29. 29. Q & A
  30. 30. Thanks pratama@kulina.id tama@fanboy.id | set.mnemonix@gmail.com https://pratamawijaya.com

×