Droidkaigi rxjava

4
-1

Published on

Droidkaigiで紹介した資料です。

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
4
On SlideShare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
1
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Droidkaigi rxjava

  1. 1. 

  2. 2.
  3. 3. 
 

  4. 4.
  5. 5.
  6. 6. 
 

  7. 7.
  8. 8. public interface Callback { void onSuccess(List<Recipe> recipes); void onError(Throwable t); } public void searchRecipe(String keyword, Callback callback) {
 recipeApiClient.search(keyword, callback);
 }
  9. 9. searchRecipe(“ステーキ”, new Callback() { @Override public void onSuccess(List<Recipe> recipes) {
 addToList(recipes); } @Override public void onError(Throwable t) { showErrorView(t); } });
 } ! ! ! !
  10. 10. searchRecipe(“ステーキ”, new Callback() { @Override public void onSuccess(List<Recipe> recipes) {
 addToList(recipes); } @Override public void onError(Throwable t) { showErrorView(t); } });
 } "
  11. 11. 
 # app/build.gradle
 dependencies {
 compile 'io.reactivex:rxjava:1.1.1' compile 'io.reactivex:rxandroid:1.1.0'
 }
  12. 12. public void searchRecipe(String keyword, Callback callback) {
 recipeApiClient.search(keyword, callback);
 }
  13. 13. 
 public void searchRecipe(String keyword) {
 recipeApiClient.search(keyword, new Callback() { @Override public void onSuccess(List<Recipe> recipes) { } @Override public void onError(Throwable t) { } });
 }
  14. 14. 
 public Observable<List<Recipe>> searchRecipe(String keyword) {
 recipeApiClient.search(keyword, new Callback() { @Override public void onSuccess(List<Recipe> recipes) {
 } @Override public void onError(Throwable t) {
 } });
 }
  15. 15. public Observable<List<Recipe>> searchRecipe(String keyword) { return Observable.create(subscriber -> {
 
 
 
 
 
 
 
 
 
 
 
 }); } void 
 recipeApiClient.search(keyword, new Callback() {
 @Override
 public void onSuccess(List<Recipe> recipes) {
 
 
 } @Override public void onError(Throwable t) {
 }
 });
  16. 16. public Observable<List<Recipe>> searchRecipe(String keyword) { return Observable.create(subscriber -> {
 recipeApiClient.search(keyword, new Callback() {
 @Override
 public void onSuccess(List<Recipe> recipes) {
 
 
 } @Override public void onError(Throwable t) { 
 } }); }); } void
  17. 17. public Observable<List<Recipe>> searchRecipe(String keyword) { return Observable.create(subscriber -> {
 recipeApiClient.search(keyword, new Callback() {
 @Override
 public void onSuccess(List<Recipe> recipes) {
 subscriber.onNext(recipes);// イベントの通知 subscriber.onCompleted(); // 全イベントが終了した通知
 } @Override public void onError(Throwable t) { subscriber.onError(t); // 処理内で失敗した通知 } }); }); } void
  18. 18. public List<Recipe> searchRecipe(String keyword) throws HttpException { // okhttpとかHttpClientで同期通信
 } 
 public Observable<List<Recipe>> searchRecipe(String keyword) { return Observable.create(subscriber -> { try{
 List<Recipe> recipes = ApiClient.search(keyword); subscriber.onNext(recipes); subscriber.onFinished(); } catch(HttpException e) {
 subscriber.onError(e);
 } }
  19. 19. searchRecipe(“ステーキ”) .subscribe(new Subscriber<List<Recipe>>() {
 @Override
 public void onNext(List<Recipe> recipe) {
 addToList(recipes);
 }
 @Override
 public void onError(Throwable e) {
 showErrorMessage(t);
 }
 
 @Override
 public void onCompleted() { 
 }); ); ! ! ! !
  20. 20. searchRecipe(“ステーキ”) .subscribe(recipes -> { addToList(recipes); }, throwable -> { showErrorMessage(t); }, () -> {
 // 省略可能
 } ); ! ! ! ! 🙇
  21. 21. searchRecipeBy(“ステーキ”)
 .map(recipes -> { /* todo something */ })
 .subscribeOn(Schedulers.io())
 .map(recipes -> { /* todo something */ }) .subscribe(recipes -> {});
  22. 22. searchRecipeBy(“ステーキ”)
 .map(recipes -> { /* todo something */ })
 .subscribeOn(Schedulers.io())
 .map(recipes -> { /* todo something */ }) .subscribe(recipes -> {}); 

  23. 23. searchRecipeBy(“ステーキ”)
 .map(recipes -> { /* todo something */ })
 .subscribeOn(Schedulers.io())
 .observeOn(AndroidSchedulers.mainThread())
 .map(recipes -> { /* todo something */ }) .observeOn(Schedulers.io()) .subscribe(recipes -> {});
  24. 24. searchRecipeBy(“ステーキ”)
 .map(recipes -> { /* todo something */ })
 .subscribeOn(Schedulers.io())
 .observeOn(AndroidSchedulers.mainThread())
 .map(recipes -> { /* todo something */ }) .observeOn(Schedulers.io()) .subscribe(recipes -> {});
  25. 25. searchRecipeBy(“ステーキ”)
 .map(recipes -> { /* todo something */ })
 .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(recipes -> {}); 

  26. 26. searchRecipe(“ステーキ”)
 .subscribeOn(schedulers.io())
 .observeOn(AndroidSchedulers.mainThread()) .subscribe(recipes -> { addToList(recipes); }, throwable -> { showErrorMessage(t); } ); ! ! ! !
  27. 27.
  28. 28. searchRecipeBy(“ハンバーグ”, new Callback() { @Override public void onSuccess(List<Recipe> recipes) {
 if(getActivity() == null) { return; }
 addToList(recipes); } @Override public void onError(Throwable t) {
 if(getActivity() == null) { return; } showErrorMessage(t); } });
 }
  29. 29. 
 # app/build.gradle
 dependencies {
 compile 'com.trello:rxlifecycle:0.4.0' compile 'com.trello:rxlifecycle-components:0.4.0'
 }
  30. 30. searchRecipeBy(“ハンバーグ”) .compose(bindToLifecycle())
 .subscribeOn(schedulers.io())
 .observeOn(AndroidSchedulers.mainThread()) .subscribe(recipes -> { addList(recipes); }, throwable -> { showErrorMessage(t); } ); ! ! ! !
  31. 31. searchRecipeBy(“ハンバーグ”) .compose(bindToLifecycle())
 .subscribeOn(schedulers.io())
 .observeOn(AndroidSchedulers.mainThread()) .subscribe(recipes -> { addList(recipes); }, throwable -> { showErrorMessage(t); } ); 

  32. 32.
  33. 33.
  34. 34. # # # # ! ! !
  35. 35. ! ! !
  36. 36. ! ! !
  37. 37. Observable.combineLatest(
 Observable.just(1),
 Observable.just(2),
 (i1,i2) -> i1 + i2 // この場合、3が次のオペレーターに渡る
 );
  38. 38. Observable.combineLatest(
 Observable.just(1),
 Observable.just(2),
 (i1,i2) -> Pair.create(i1, i2)
 ); Observable.combineLatest(
 Observable.just(1),
 Observable.just(2),
 Pair::create
 );
  39. 39. Observable.combineLatist( searchRecipe(“ステーキ”), searchAd(“ステーキ”), // 広告情報を取得するメソッドだと思って下さい Pair::create )
 .subscribeOn(schedulers.io())
 .observeOn(AndroidSchedulers.mainThread()) .subscribe(pair -> { addToListWithBannerIfExist(pair.first, pair.second); }, throwable -> { showErrorMessage(t); } );
  40. 40. 😱
  41. 41. AdObservable(“タマゴ”) .onErrorReturn(throwable -> AdModel.EmptyData());
  42. 42. public Observable<AdModel> searchAd(String keyword) { return Observable.create(subscriber -> { try{
 AdModel adModel = adClient.search(keyword); subscriber.onNext(adModel); } catch(HttpException e) {
 subscriber.onError(e);
 }) .onErrorReturn(e -> { return AdModel.createEmptyData();
 }); }
  43. 43. Observable.combineLatist( searchRecipe(“ステーキ”), searchAd(“ステーキ”), Pair::create )
 .subscribeOn(schedulers.io())
 .observeOn(AndroidSchedulers.mainThread()) .subscribe( /* 省略 */ );
  44. 44. Observable.combineLatist( searchRecipe(“ステーキ”).subscribeOn(schedulers.io()), searchAd(“ステーキ”).subscribeOn(schedulers.io()), Pair::create )
 .subscribeOn(schedulers.io())
 .observeOn(AndroidSchedulers.mainThread()) .subscribe( /* 省略 */ );
  45. 45.
  46. 46. AdObservable(“タマゴ”) .retry(1); // 例外が発生した場合1回だけリトライする AdObservable(“タマゴ”) .retry((count, throwable) -> count > 2)
  47. 47. 
 public Observable<AdModel> searchAd(String keyword) { return Observable.create(subscriber -> { try{
 AdModel ad = AdClient.search(keyword); subscriber.onNext(ad); } catch(HttpException e) {
 subscriber.onError(e);
 }) .retry(1) .onErrorReturn(e -> { return AdModel.createEmptyData();
 }); }
  48. 48. ! ! ! ♥ ♥ ♥ 

  49. 49. ! ! ! ♥ ♥ ♥
  50. 50. searchRecipe(“ステーキ”) .flatMap(recipes -> fetchLikedRecipeIds(recipes)) .subscribe(recipeIds -> {})
  51. 51. searchRecipe(“ステーキ”)
 .flatMap(recipes -> 
 Observable.combineLatist( Observable.just(recipes), // レシピリストをそのまま渡す
 fetchLikedRecipe(recipes)), //レシピリストからLikeしたレシピIdを取得 Pair::create
 )
 )
 .map(this::mergeLikeState) // mapでLike状態をレシピインスタンスに反映させる(省略) .subscribe(recipes -> {})
  52. 52. public Observable<List<Recipe>> searchRecipeWithLikeState(String keyword) { searchRecipe(keyword) .flatMap(recipes -> Observable.combineLatist( Observable.just(recipes), fetchLikedRecipeIds(recipes)), Pair::create ) ) .map(this::mergeLikeState) }
 public Func1<List<Recipe>> mergeLikeState(Pair<List<Recipe>, Integer> pair) {
 // Like状態をレシピインスタンスに反映させる(省略)
 }
  53. 53. ! ! ! ♥ ♥ ♥
  54. 54. Observable.combineLatist( searchRecipeWithLikeState(“ステーキ”) .subscribeOn(schedulers.io()), searchAd(“ステーキ”) .subscribeOn(schedulers.io()), Pair::create ) .compose(bindToLifecycle())
 .subscribeOn(schedulers.io())
 .observeOn(AndroidSchedulers.mainThread()) .subscribe(pair -> { addToList(pair.first, pair.second); }, throwable -> { showErrorMessage(t); } ); ! ! ! ♥ ♥ ♥
  55. 55. Observable.combineLatest(
 Observable.just(1),
 Observable.just(2),
 (i1,i2) -> i1 + i2 // この場合、3が次のオペレーターに渡る
 ); 

  56. 56. searchRecipe(“ステーキ”) .flatMap(recipes -> recipeClient.fetchLikedRecipeIds(recipes)) .subscribe(recipeIds -> {})
  57. 57. searchRecipe(“ステーキ”)
 .flatMap(recipes -> 
 Observable.combineLatist( Observable.just(recipes), 
 recipeClient.fetchLikedRecipe(recipes)),
 Pair::create
 )
 )
 .map(this::mergeLikeState) 
 .subscribe(recipes -> {})
  58. 58. 
 
 public class Tuple3<T1, T2, T3> {
 public T1 first; public T2 second; public T3 third;
 public Tuple3(T1 first, T2 second, T3 third) {
 this.first = first;
 this.second = second;
 this.third = third;
 }
 public static <T1, T2, T3> Tuple3<T1, T2, T3> create(T1 t1, T2 t2, T3 t3) {
 return new Tuple3<>(t1, t2, t3);
 }
 }
  59. 59. // in your CustomApplication public void onCreate(Bundle saveInstanceState){
 super.onCreate(); System.setProperty("rx.ring-buffer.size", "32"); }
  60. 60. Single.create(subscriber -> {
 subscriber.onSuccess("Hello world");
 }).subscribe(text -> {
 System.out.println(text);
 }, throwable -> {
 Crashlytics.logException(e);
 });
  61. 61. 
 Completable.create(subscriber -> { 
 subscriber.onCompleted()
 }).subscribe(throwable -> {
 Crashlytics.logException(e);
 }, () -> {
 
 });
  62. 62.

×