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.


















public interface Callback {
void onSuccess(List<Recipe> recipes);
void onError(Throwable t);
}
public void searchRecipe(St...
searchRecipe(“ステーキ”, new Callback() {
@Override
public void onSuccess(List<Recipe> recipes) {

addToList(recipes);
}
@Over...
searchRecipe(“ステーキ”, new Callback() {
@Override
public void onSuccess(List<Recipe> recipes) {

addToList(recipes);
}
@Over...
searchRecipe(“ステーキ”, new Callback() {
@Override
public void onSuccess(List<Recipe> recipes) {

addToList(recipes);
}
@Over...
searchRecipe(“ステーキ”, new Callback() {
@Override
public void onSuccess(List<Recipe> recipes) {

addToList(recipes);
}
@Over...
searchRecipe(“ステーキ”, new Callback() {
@Override
public void onSuccess(List<Recipe> recipes) {

addToList(recipes);
}
@Over...
searchRecipe(“ステーキ”, new Callback() {
@Override
public void onSuccess(List<Recipe> recipes) {

addToList(recipes);
}
@Over...
searchRecipe(“ステーキ”, new Callback() {
@Override
public void onSuccess(List<Recipe> recipes) {

addToList(recipes);
}
@Over...
searchRecipe(“ステーキ”, new Callback() {
@Override
public void onSuccess(List<Recipe> recipes) {

addToList(recipes);
}
@Over...
searchRecipe(“ステーキ”, new Callback() {
@Override
public void onSuccess(List<Recipe> recipes) {

addToList(recipes);
}
@Over...
searchRecipe(“ステーキ”, new Callback() {
@Override
public void onSuccess(List<Recipe> recipes) {

addToList(recipes);
}
@Over...
searchRecipe(“ステーキ”, new Callback() {
@Override
public void onSuccess(List<Recipe> recipes) {

addToList(recipes);
}
@Over...
searchRecipe(“ステーキ”, new Callback() {
@Override
public void onSuccess(List<Recipe> recipes) {

addToList(recipes);
}
@Over...
searchRecipe(“ステーキ”, new Callback() {
@Override
public void onSuccess(List<Recipe> recipes) {

addToList(recipes);
}
@Over...


# app/build.gradle

dependencies {

compile 'io.reactivex:rxjava:1.1.1'
compile 'io.reactivex:rxandroid:1.1.0'

}
public void searchRecipe(String keyword, Callback callback) {

recipeApiClient.search(keyword, callback);

}
public void searchRecipe(String keyword, Callback callback) {

recipeApiClient.search(keyword, callback);

}
public void searchRecipe(String keyword, Callback callback) {

recipeApiClient.search(keyword, callback);

}


public void searchRecipe(String keyword) {

recipeApiClient.search(keyword, new Callback() {
@Override
public void onSuc...


public void searchRecipe(String keyword) {

recipeApiClient.search(keyword, new Callback() {
@Override
public void onSuc...


public Observable<List<Recipe>> searchRecipe(String keyword) {

recipeApiClient.search(keyword, new Callback() {
@Overri...


public Observable<List<Recipe>> searchRecipe(String keyword) {

recipeApiClient.search(keyword, new Callback() {
@Overri...
public Observable<List<Recipe>> searchRecipe(String keyword) {
return Observable.create(subscriber -> {


















...
public Observable<List<Recipe>> searchRecipe(String keyword) {
return Observable.create(subscriber -> {

recipeApiClient.s...
public Observable<List<Recipe>> searchRecipe(String keyword) {
return Observable.create(subscriber -> {

recipeApiClient.s...
public Observable<List<Recipe>> searchRecipe(String keyword) {
return Observable.create(subscriber -> {

recipeApiClient.s...
public Observable<List<Recipe>> searchRecipe(String keyword) {
return Observable.create(subscriber -> {

recipeApiClient.s...
public List<Recipe> searchRecipe(String keyword) throws HttpException {
// okhttpとかHttpClientで同期通信

}


public Observable<...
searchRecipe(“ステーキ”)
.subscribe(new Subscriber<List<Recipe>>() {

@Override

public void onNext(List<Recipe> recipe) {

ad...
searchRecipe(“ステーキ”)
.subscribe(new Subscriber<List<Recipe>>() {

@Override

public void onNext(List<Recipe> recipe) {

ad...
searchRecipe(“ステーキ”)
.subscribe(new Subscriber<List<Recipe>>() {

@Override

public void onNext(List<Recipe> recipe) {

ad...
searchRecipe(“ステーキ”)
.subscribe(new Subscriber<List<Recipe>>() {

@Override

public void onNext(List<Recipe> recipe) {

ad...
searchRecipe(“ステーキ”)
.subscribe(new Subscriber<List<Recipe>>() {

@Override

public void onNext(List<Recipe> recipe) {

ad...
searchRecipe(“ステーキ”)
.subscribe(new Subscriber<List<Recipe>>() {

@Override

public void onNext(List<Recipe> recipe) {

ad...
searchRecipe(“ステーキ”)
.subscribe(new Subscriber<List<Recipe>>() {

@Override

public void onNext(List<Recipe> recipe) {

ad...
searchRecipe(“ステーキ”)
.subscribe(new Subscriber<List<Recipe>>() {

@Override

public void onNext(List<Recipe> recipe) {

ad...
searchRecipe(“ステーキ”)
.subscribe(recipes -> {
addToList(recipes);
},
throwable -> {
showErrorMessage(t);
},
() -> {

// 省略可...
searchRecipe(“ステーキ”)
.subscribe(recipes -> {
addToList(recipes);
},
throwable -> {
showErrorMessage(t);
},
() -> {

// 省略可...
searchRecipe(“ステーキ”)
.subscribe(recipes -> {
addToList(recipes);
},
throwable -> {
showErrorMessage(t);
},
() -> {

// 省略可...
searchRecipe(“ステーキ”)
.subscribe(recipes -> {
addToList(recipes);
},
throwable -> {
showErrorMessage(t);
},
() -> {

// 省略可...
🙇
searchRecipeBy(“ステーキ”)

.map(recipes -> { /* todo something */ })

.subscribeOn(Schedulers.io())

.map(recipes -> { /* tod...
searchRecipeBy(“ステーキ”)

.map(recipes -> { /* todo something */ })

.subscribeOn(Schedulers.io())

.map(recipes -> { /* tod...
searchRecipeBy(“ステーキ”)

.map(recipes -> { /* todo something */ })

.subscribeOn(Schedulers.io())

.observeOn(AndroidSchedu...
searchRecipeBy(“ステーキ”)

.map(recipes -> { /* todo something */ })

.subscribeOn(Schedulers.io())

.observeOn(AndroidSchedu...
searchRecipeBy(“ステーキ”)

.map(recipes -> { /* todo something */ })

.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedul...
searchRecipe(“ステーキ”)

.subscribeOn(schedulers.io())

.observeOn(AndroidSchedulers.mainThread())
.subscribe(recipes -> {
ad...
searchRecipe(“ステーキ”)

.subscribeOn(schedulers.io())

.observeOn(AndroidSchedulers.mainThread())
.subscribe(recipes -> {
ad...
searchRecipe(“ステーキ”)

.subscribeOn(schedulers.io())

.observeOn(AndroidSchedulers.mainThread())
.subscribe(recipes -> {
ad...
searchRecipe(“ステーキ”)

.subscribeOn(schedulers.io())

.observeOn(AndroidSchedulers.mainThread())
.subscribe(recipes -> {
ad...


searchRecipeBy(“ハンバーグ”, new Callback() {
@Override
public void onSuccess(List<Recipe> recipes) {

if(getActivity() == null...


# app/build.gradle

dependencies {

compile 'com.trello:rxlifecycle:0.4.0'
compile 'com.trello:rxlifecycle-components:0....
searchRecipeBy(“ハンバーグ”)
.compose(bindToLifecycle())

.subscribeOn(schedulers.io())

.observeOn(AndroidSchedulers.mainThrea...
searchRecipeBy(“ハンバーグ”)
.compose(bindToLifecycle())

.subscribeOn(schedulers.io())

.observeOn(AndroidSchedulers.mainThrea...
searchRecipeBy(“ハンバーグ”)
.compose(bindToLifecycle())

.subscribeOn(schedulers.io())

.observeOn(AndroidSchedulers.mainThrea...
searchRecipeBy(“ハンバーグ”)
.compose(bindToLifecycle())

.subscribeOn(schedulers.io())

.observeOn(AndroidSchedulers.mainThrea...
searchRecipeBy(“ハンバーグ”)
.compose(bindToLifecycle())

.subscribeOn(schedulers.io())

.observeOn(AndroidSchedulers.mainThrea...
searchRecipeBy(“ハンバーグ”)
.compose(bindToLifecycle())

.subscribeOn(schedulers.io())

.observeOn(AndroidSchedulers.mainThrea...
searchRecipeBy(“ハンバーグ”)
.compose(bindToLifecycle())

.subscribeOn(schedulers.io())

.observeOn(AndroidSchedulers.mainThrea...
searchRecipeBy(“ハンバーグ”)
.compose(bindToLifecycle())

.subscribeOn(schedulers.io())

.observeOn(AndroidSchedulers.mainThrea...




#
#
#
#
!
!
!
#
#
#
#
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
Observable.combineLatest(

Observable.just(1),

Observable.just(2),

(i1,i2) -> i1 + i2 // この場合、3が次のオペレーターに渡る

);
Observable.combineLatest(

Observable.just(1),

Observable.just(2),

(i1,i2) -> Pair.create(i1, i2)

);
Observable.combine...
Observable.combineLatest(
searchRecipe(“ステーキ”),
searchAd(“ステーキ”), // 広告情報を取得するメソッドだと思って下さい
Pair::create
)

.subscribeOn(sc...
Observable.combineLatest(
searchRecipe(“ステーキ”),
searchAd(“ステーキ”), // 広告情報を取得するメソッドだと思って下さい
Pair::create
)

.subscribeOn(sc...
Observable.combineLatest(
searchRecipe(“ステーキ”),
searchAd(“ステーキ”), // 広告情報を取得するメソッドだと思って下さい
Pair::create
)

.subscribeOn(sc...
Observable.combineLatest(
searchRecipe(“ステーキ”),
searchAd(“ステーキ”), // 広告情報を取得するメソッドだと思って下さい
Pair::create
)

.subscribeOn(sc...
Observable.combineLatest(
searchRecipe(“ステーキ”),
searchAd(“ステーキ”), // 広告情報を取得するメソッドだと思って下さい
Pair::create
)

.subscribeOn(sc...
Observable.combineLatest(
searchRecipe(“ステーキ”),
searchAd(“ステーキ”), // 広告情報を取得するメソッドだと思って下さい
Pair::create
)

.subscribeOn(sc...
Observable.combineLatest(
searchRecipe(“ステーキ”),
searchAd(“ステーキ”), // 広告情報を取得するメソッドだと思って下さい
Pair::create
)

.subscribeOn(sc...
Observable.combineLatest(
searchRecipe(“ステーキ”),
searchAd(“ステーキ”), // 広告情報を取得するメソッドだと思って下さい
Pair::create
)

.subscribeOn(sc...
😱
😱
😱
AdObservable(“タマゴ”)
.onErrorReturn(throwable -> AdModel.EmptyData());
public Observable<AdModel> searchAd(String keyword) {
return Observable.create(subscriber -> {
try{

AdModel adModel = adC...
public Observable<AdModel> searchAd(String keyword) {
return Observable.create(subscriber -> {
try{

AdModel adModel = adC...
public Observable<AdModel> searchAd(String keyword) {
return Observable.create(subscriber -> {
try{

AdModel adModel = adC...
public Observable<AdModel> searchAd(String keyword) {
return Observable.create(subscriber -> {
try{

AdModel adModel = adC...
public Observable<AdModel> searchAd(String keyword) {
return Observable.create(subscriber -> {
try{

AdModel adModel = adC...
Observable.combineLatest(
searchRecipe(“ステーキ”),
searchAd(“ステーキ”),
Pair::create
)

.subscribeOn(schedulers.io())

.observeO...
Observable.combineLatest(
searchRecipe(“ステーキ”),
searchAd(“ステーキ”),
Pair::create
)

.subscribeOn(schedulers.io())

.observeO...
Observable.combineLatest(
searchRecipe(“ステーキ”),
searchAd(“ステーキ”),
Pair::create
)

.subscribeOn(schedulers.io())

.observeO...
Observable.combineLatest(
searchRecipe(“ステーキ”),
searchAd(“ステーキ”),
Pair::create
)

.subscribeOn(schedulers.io())

.observeO...
Observable.combineLatest(
searchRecipe(“ステーキ”),
searchAd(“ステーキ”),
Pair::create
)

.subscribeOn(schedulers.io())

.observeO...
Observable.combineLatest(
searchRecipe(“ステーキ”).subscribeOn(schedulers.io()),
searchAd(“ステーキ”).subscribeOn(schedulers.io())...


AdObservable(“タマゴ”)
.retry(1); // 例外が発生した場合1回だけリトライする
AdObservable(“タマゴ”)
.retry((count, throwable) -> count > 2)


public Observable<AdModel> searchAd(String keyword) {
return Observable.create(subscriber -> {
try{

AdModel ad = AdClie...
!
!
!


!
!
!
♥
♥
♥


!
!
!
♥
♥
♥
!
!
!
♥
♥
♥
searchRecipe(“ステーキ”)
.flatMap(recipes -> fetchLikedRecipeIds(recipes))
.subscribe(recipeIds -> {})
searchRecipe(“ステーキ”)
.flatMap(recipes -> fetchLikedRecipeIds(recipes))
.subscribe(recipeIds -> {})
searchRecipe(“ステーキ”)
.flatMap(recipes -> fetchLikedRecipeIds(recipes))
.subscribe(recipeIds -> {})
searchRecipe(“ステーキ”)
.flatMap(recipes -> fetchLikedRecipeIds(recipes))
.subscribe(recipeIds -> {})
searchRecipe(“ステーキ”)

.flatMap(recipes -> 

Observable.combineLatest(
Observable.just(recipes), // レシピリストをそのまま渡す

fetchLik...
searchRecipe(“ステーキ”)

.flatMap(recipes -> 

Observable.combineLatest(
Observable.just(recipes), // レシピリストをそのまま渡す

fetchLik...
searchRecipe(“ステーキ”)

.flatMap(recipes -> 

Observable.combineLatest(
Observable.just(recipes), // レシピリストをそのまま渡す

fetchLik...
searchRecipe(“ステーキ”)

.flatMap(recipes -> {

fetchLikedRecipe(recipes)

.map(likeIds -> {

mergeLikeState(recipes, likeIds...
public Observable<List<Recipe>> searchRecipeWithLikeState(String keyword) {
searchRecipe(keyword)
.flatMap(recipes ->
Obse...
!
!
!
♥
♥
♥
!
!
!
♥
♥
♥
!
!
!
♥
♥
♥
!
!
!
♥
♥
♥
!
!
!
♥
♥
♥
!
!
!
♥
♥
♥
!
!
!
♥
♥
♥
!
!
!
♥
♥
♥
!
!
!
♥
♥
♥
!
!
!
♥
♥
♥
!
!
!
♥
♥
♥
!
!
!
♥
♥
♥
!
!
!
♥
♥
♥
Observable.combineLatest(
searchRecipeWithLikeState(“ステーキ”)
.subscribeOn(schedulers.io()),
searchAd(“ステーキ”)
.subscribeOn(s...
Observable.combineLatest(

Observable.just(1),

Observable.just(2),

(i1,i2) -> i1 + i2

);


searchRecipe(“ステーキ”)
.flatMap(recipes -> recipeClient.fetchLikedRecipeIds(recipes))
.subscribe(recipeIds -> {})


public class Tuple3<T1, T2, T3> {

public T1 first; public T2 second; public T3 third;

public Tuple3(T1 first, T2 secon...
// in your CustomApplication
public void onCreate(Bundle saveInstanceState){

super.onCreate();
System.setProperty("rx.rin...
Single.create(subscriber -> {

subscriber.onSuccess("Hello world");

}).subscribe(text -> {

System.out.println(text);

},...


Completable.create(subscriber -> { 

subscriber.onCompleted()

}).subscribe(throwable -> {

Crashlytics.logException(e);...


明日から使えるRxjava頻出パターン (Droid kaigi 2016)
明日から使えるRxjava頻出パターン (Droid kaigi 2016)
明日から使えるRxjava頻出パターン (Droid kaigi 2016)
明日から使えるRxjava頻出パターン (Droid kaigi 2016)
明日から使えるRxjava頻出パターン (Droid kaigi 2016)
明日から使えるRxjava頻出パターン (Droid kaigi 2016)
明日から使えるRxjava頻出パターン (Droid kaigi 2016)
明日から使えるRxjava頻出パターン (Droid kaigi 2016)
明日から使えるRxjava頻出パターン (Droid kaigi 2016)
明日から使えるRxjava頻出パターン (Droid kaigi 2016)
明日から使えるRxjava頻出パターン (Droid kaigi 2016)
明日から使えるRxjava頻出パターン (Droid kaigi 2016)
明日から使えるRxjava頻出パターン (Droid kaigi 2016)
明日から使えるRxjava頻出パターン (Droid kaigi 2016)
明日から使えるRxjava頻出パターン (Droid kaigi 2016)
明日から使えるRxjava頻出パターン (Droid kaigi 2016)
明日から使えるRxjava頻出パターン (Droid kaigi 2016)
明日から使えるRxjava頻出パターン (Droid kaigi 2016)
明日から使えるRxjava頻出パターン (Droid kaigi 2016)
明日から使えるRxjava頻出パターン (Droid kaigi 2016)
明日から使えるRxjava頻出パターン (Droid kaigi 2016)
明日から使えるRxjava頻出パターン (Droid kaigi 2016)
明日から使えるRxjava頻出パターン (Droid kaigi 2016)
Upcoming SlideShare
Loading in …5
×

明日から使えるRxjava頻出パターン (Droid kaigi 2016)

19,656 views

Published on

Droidkaigiで紹介した資料です。

Published in: Technology
  • Be the first to comment

明日から使えるRxjava頻出パターン (Droid kaigi 2016)

  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. searchRecipe(“ステーキ”, new Callback() { @Override public void onSuccess(List<Recipe> recipes) {
 addToList(recipes); } @Override public void onError(Throwable t) { showErrorView(t); } });
 }
  12. 12. searchRecipe(“ステーキ”, new Callback() { @Override public void onSuccess(List<Recipe> recipes) {
 addToList(recipes); } @Override public void onError(Throwable t) { showErrorView(t); } });
 }
  13. 13. searchRecipe(“ステーキ”, new Callback() { @Override public void onSuccess(List<Recipe> recipes) {
 addToList(recipes); } @Override public void onError(Throwable t) { showErrorView(t); } });
 } !
  14. 14. searchRecipe(“ステーキ”, new Callback() { @Override public void onSuccess(List<Recipe> recipes) {
 addToList(recipes); } @Override public void onError(Throwable t) { showErrorView(t); } });
 } ! !
  15. 15. searchRecipe(“ステーキ”, new Callback() { @Override public void onSuccess(List<Recipe> recipes) {
 addToList(recipes); } @Override public void onError(Throwable t) { showErrorView(t); } });
 } ! ! !
  16. 16. searchRecipe(“ステーキ”, new Callback() { @Override public void onSuccess(List<Recipe> recipes) {
 addToList(recipes); } @Override public void onError(Throwable t) { showErrorView(t); } });
 } ! ! ! !
  17. 17. searchRecipe(“ステーキ”, new Callback() { @Override public void onSuccess(List<Recipe> recipes) {
 addToList(recipes); } @Override public void onError(Throwable t) { showErrorView(t); } });
 }
  18. 18. searchRecipe(“ステーキ”, new Callback() { @Override public void onSuccess(List<Recipe> recipes) {
 addToList(recipes); } @Override public void onError(Throwable t) { showErrorView(t); } });
 }
  19. 19. searchRecipe(“ステーキ”, new Callback() { @Override public void onSuccess(List<Recipe> recipes) {
 addToList(recipes); } @Override public void onError(Throwable t) { showErrorView(t); } });
 }
  20. 20. searchRecipe(“ステーキ”, new Callback() { @Override public void onSuccess(List<Recipe> recipes) {
 addToList(recipes); } @Override public void onError(Throwable t) { showErrorView(t); } });
 }
  21. 21. searchRecipe(“ステーキ”, new Callback() { @Override public void onSuccess(List<Recipe> recipes) {
 addToList(recipes); } @Override public void onError(Throwable t) { showErrorView(t); } });
 } "
  22. 22. 
 # app/build.gradle
 dependencies {
 compile 'io.reactivex:rxjava:1.1.1' compile 'io.reactivex:rxandroid:1.1.0'
 }
  23. 23. public void searchRecipe(String keyword, Callback callback) {
 recipeApiClient.search(keyword, callback);
 }
  24. 24. public void searchRecipe(String keyword, Callback callback) {
 recipeApiClient.search(keyword, callback);
 }
  25. 25. public void searchRecipe(String keyword, Callback callback) {
 recipeApiClient.search(keyword, callback);
 }
  26. 26. 
 public void searchRecipe(String keyword) {
 recipeApiClient.search(keyword, new Callback() { @Override public void onSuccess(List<Recipe> recipes) { } @Override public void onError(Throwable t) { } });
 }
  27. 27. 
 public void searchRecipe(String keyword) {
 recipeApiClient.search(keyword, new Callback() { @Override public void onSuccess(List<Recipe> recipes) { } @Override public void onError(Throwable t) { } });
 }
  28. 28. 
 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) {
 } });
 }
  29. 29. 
 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) {
 } });
 }
  30. 30. 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) {
 }
 });
  31. 31. 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
  32. 32. 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
  33. 33. 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
  34. 34. 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
  35. 35. 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);
 } }
  36. 36. 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() { 
 }); );
  37. 37. 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() { 
 }); );
  38. 38. 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() { 
 }); );
  39. 39. 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() { 
 }); );
  40. 40. 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() { 
 }); ); !
  41. 41. 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() { 
 }); ); ! !
  42. 42. 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() { 
 }); ); ! ! !
  43. 43. 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() { 
 }); ); ! ! ! !
  44. 44. searchRecipe(“ステーキ”) .subscribe(recipes -> { addToList(recipes); }, throwable -> { showErrorMessage(t); }, () -> {
 // 省略可能
 } );
  45. 45. searchRecipe(“ステーキ”) .subscribe(recipes -> { addToList(recipes); }, throwable -> { showErrorMessage(t); }, () -> {
 // 省略可能
 } );
  46. 46. searchRecipe(“ステーキ”) .subscribe(recipes -> { addToList(recipes); }, throwable -> { showErrorMessage(t); }, () -> {
 // 省略可能
 } );
  47. 47. searchRecipe(“ステーキ”) .subscribe(recipes -> { addToList(recipes); }, throwable -> { showErrorMessage(t); }, () -> {
 // 省略可能
 } ); ! ! ! !
  48. 48. 🙇
  49. 49. searchRecipeBy(“ステーキ”)
 .map(recipes -> { /* todo something */ })
 .subscribeOn(Schedulers.io())
 .map(recipes -> { /* todo something */ }) .subscribe(recipes -> {});
  50. 50. searchRecipeBy(“ステーキ”)
 .map(recipes -> { /* todo something */ })
 .subscribeOn(Schedulers.io())
 .map(recipes -> { /* todo something */ }) .subscribe(recipes -> {}); 

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

  54. 54. searchRecipe(“ステーキ”)
 .subscribeOn(schedulers.io())
 .observeOn(AndroidSchedulers.mainThread()) .subscribe(recipes -> { addToList(recipes); }, throwable -> { showErrorMessage(t); } );
  55. 55. searchRecipe(“ステーキ”)
 .subscribeOn(schedulers.io())
 .observeOn(AndroidSchedulers.mainThread()) .subscribe(recipes -> { addToList(recipes); }, throwable -> { showErrorMessage(t); } );
  56. 56. searchRecipe(“ステーキ”)
 .subscribeOn(schedulers.io())
 .observeOn(AndroidSchedulers.mainThread()) .subscribe(recipes -> { addToList(recipes); }, throwable -> { showErrorMessage(t); } );
  57. 57. searchRecipe(“ステーキ”)
 .subscribeOn(schedulers.io())
 .observeOn(AndroidSchedulers.mainThread()) .subscribe(recipes -> { addToList(recipes); }, throwable -> { showErrorMessage(t); } ); ! ! ! !
  58. 58.
  59. 59. 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); } });
 }
  60. 60. 
 # app/build.gradle
 dependencies {
 compile 'com.trello:rxlifecycle:0.4.0' compile 'com.trello:rxlifecycle-components:0.4.0'
 }
  61. 61. searchRecipeBy(“ハンバーグ”) .compose(bindToLifecycle())
 .subscribeOn(schedulers.io())
 .observeOn(AndroidSchedulers.mainThread()) .subscribe(recipes -> { addList(recipes); }, throwable -> { showErrorMessage(t); } );
  62. 62. searchRecipeBy(“ハンバーグ”) .compose(bindToLifecycle())
 .subscribeOn(schedulers.io())
 .observeOn(AndroidSchedulers.mainThread()) .subscribe(recipes -> { addList(recipes); }, throwable -> { showErrorMessage(t); } );
  63. 63. searchRecipeBy(“ハンバーグ”) .compose(bindToLifecycle())
 .subscribeOn(schedulers.io())
 .observeOn(AndroidSchedulers.mainThread()) .subscribe(recipes -> { addList(recipes); }, throwable -> { showErrorMessage(t); } );
  64. 64. searchRecipeBy(“ハンバーグ”) .compose(bindToLifecycle())
 .subscribeOn(schedulers.io())
 .observeOn(AndroidSchedulers.mainThread()) .subscribe(recipes -> { addList(recipes); }, throwable -> { showErrorMessage(t); } ); ! ! ! !
  65. 65. searchRecipeBy(“ハンバーグ”) .compose(bindToLifecycle())
 .subscribeOn(schedulers.io())
 .observeOn(AndroidSchedulers.mainThread()) .subscribe(recipes -> { addList(recipes); }, throwable -> { showErrorMessage(t); } );
  66. 66. searchRecipeBy(“ハンバーグ”) .compose(bindToLifecycle())
 .subscribeOn(schedulers.io())
 .observeOn(AndroidSchedulers.mainThread()) .subscribe(recipes -> { addList(recipes); }, throwable -> { showErrorMessage(t); } );
  67. 67. searchRecipeBy(“ハンバーグ”) .compose(bindToLifecycle())
 .subscribeOn(schedulers.io())
 .observeOn(AndroidSchedulers.mainThread()) .subscribe(recipes -> { addList(recipes); }, throwable -> { showErrorMessage(t); } ); 

  68. 68. searchRecipeBy(“ハンバーグ”) .compose(bindToLifecycle())
 .subscribeOn(schedulers.io())
 .observeOn(AndroidSchedulers.mainThread()) .subscribe(recipes -> { addList(recipes); }, throwable -> { showErrorMessage(t); } ); 

  69. 69.
  70. 70.
  71. 71. # # # # ! ! !
  72. 72. # # # # ! ! !
  73. 73. ! ! !
  74. 74. ! ! !
  75. 75. ! ! !
  76. 76. ! ! !
  77. 77. ! ! !
  78. 78. ! ! !
  79. 79. ! ! !
  80. 80. ! ! !
  81. 81. ! ! !
  82. 82. !
  83. 83. !
  84. 84. ! !
  85. 85. ! ! !
  86. 86. ! ! !
  87. 87. Observable.combineLatest(
 Observable.just(1),
 Observable.just(2),
 (i1,i2) -> i1 + i2 // この場合、3が次のオペレーターに渡る
 );
  88. 88. Observable.combineLatest(
 Observable.just(1),
 Observable.just(2),
 (i1,i2) -> Pair.create(i1, i2)
 ); Observable.combineLatest(
 Observable.just(1),
 Observable.just(2),
 Pair::create
 );
  89. 89. Observable.combineLatest( searchRecipe(“ステーキ”), searchAd(“ステーキ”), // 広告情報を取得するメソッドだと思って下さい Pair::create )
 .subscribeOn(schedulers.io())
 .observeOn(AndroidSchedulers.mainThread()) .subscribe(pair -> { addToListWithBannerIfExist(pair.first, pair.second); }, throwable -> { showErrorMessage(t); } );
  90. 90. Observable.combineLatest( searchRecipe(“ステーキ”), searchAd(“ステーキ”), // 広告情報を取得するメソッドだと思って下さい Pair::create )
 .subscribeOn(schedulers.io())
 .observeOn(AndroidSchedulers.mainThread()) .subscribe(pair -> { addToListWithBannerIfExist(pair.first, pair.second); }, throwable -> { showErrorMessage(t); } );
  91. 91. Observable.combineLatest( searchRecipe(“ステーキ”), searchAd(“ステーキ”), // 広告情報を取得するメソッドだと思って下さい Pair::create )
 .subscribeOn(schedulers.io())
 .observeOn(AndroidSchedulers.mainThread()) .subscribe(pair -> { addToListWithBannerIfExist(pair.first, pair.second); }, throwable -> { showErrorMessage(t); } );
  92. 92. Observable.combineLatest( searchRecipe(“ステーキ”), searchAd(“ステーキ”), // 広告情報を取得するメソッドだと思って下さい Pair::create )
 .subscribeOn(schedulers.io())
 .observeOn(AndroidSchedulers.mainThread()) .subscribe(pair -> { addToListWithBannerIfExist(pair.first, pair.second); }, throwable -> { showErrorMessage(t); } );
  93. 93. Observable.combineLatest( searchRecipe(“ステーキ”), searchAd(“ステーキ”), // 広告情報を取得するメソッドだと思って下さい Pair::create )
 .subscribeOn(schedulers.io())
 .observeOn(AndroidSchedulers.mainThread()) .subscribe(pair -> { addToListWithBannerIfExist(pair.first, pair.second); }, throwable -> { showErrorMessage(t); } );
  94. 94. Observable.combineLatest( searchRecipe(“ステーキ”), searchAd(“ステーキ”), // 広告情報を取得するメソッドだと思って下さい Pair::create )
 .subscribeOn(schedulers.io())
 .observeOn(AndroidSchedulers.mainThread()) .subscribe(pair -> { addToListWithBannerIfExist(pair.first, pair.second); }, throwable -> { showErrorMessage(t); } );
  95. 95. Observable.combineLatest( searchRecipe(“ステーキ”), searchAd(“ステーキ”), // 広告情報を取得するメソッドだと思って下さい Pair::create )
 .subscribeOn(schedulers.io())
 .observeOn(AndroidSchedulers.mainThread()) .subscribe(pair -> { addToListWithBannerIfExist(pair.first, pair.second); }, throwable -> { showErrorMessage(t); } );
  96. 96. Observable.combineLatest( searchRecipe(“ステーキ”), searchAd(“ステーキ”), // 広告情報を取得するメソッドだと思って下さい Pair::create )
 .subscribeOn(schedulers.io())
 .observeOn(AndroidSchedulers.mainThread()) .subscribe(pair -> { addToListWithBannerIfExist(pair.first, pair.second); }, throwable -> { showErrorMessage(t); } );
  97. 97. 😱
  98. 98. 😱
  99. 99. 😱
  100. 100. AdObservable(“タマゴ”) .onErrorReturn(throwable -> AdModel.EmptyData());
  101. 101. 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();
 }); }
  102. 102. 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();
 }); }
  103. 103. 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();
 }); }
  104. 104. 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();
 }); }
  105. 105. 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();
 }); }
  106. 106. Observable.combineLatest( searchRecipe(“ステーキ”), searchAd(“ステーキ”), Pair::create )
 .subscribeOn(schedulers.io())
 .observeOn(AndroidSchedulers.mainThread()) .subscribe( /* 省略 */ );
  107. 107. Observable.combineLatest( searchRecipe(“ステーキ”), searchAd(“ステーキ”), Pair::create )
 .subscribeOn(schedulers.io())
 .observeOn(AndroidSchedulers.mainThread()) .subscribe( /* 省略 */ );
  108. 108. Observable.combineLatest( searchRecipe(“ステーキ”), searchAd(“ステーキ”), Pair::create )
 .subscribeOn(schedulers.io())
 .observeOn(AndroidSchedulers.mainThread()) .subscribe( /* 省略 */ );
  109. 109. Observable.combineLatest( searchRecipe(“ステーキ”), searchAd(“ステーキ”), Pair::create )
 .subscribeOn(schedulers.io())
 .observeOn(AndroidSchedulers.mainThread()) .subscribe( /* 省略 */ );
  110. 110. Observable.combineLatest( searchRecipe(“ステーキ”), searchAd(“ステーキ”), Pair::create )
 .subscribeOn(schedulers.io())
 .observeOn(AndroidSchedulers.mainThread()) .subscribe( /* 省略 */ );
  111. 111. Observable.combineLatest( searchRecipe(“ステーキ”).subscribeOn(schedulers.io()), searchAd(“ステーキ”).subscribeOn(schedulers.io()), Pair::create )
 .subscribeOn(schedulers.io())
 .observeOn(AndroidSchedulers.mainThread()) .subscribe( /* 省略 */ );
  112. 112.
  113. 113. AdObservable(“タマゴ”) .retry(1); // 例外が発生した場合1回だけリトライする AdObservable(“タマゴ”) .retry((count, throwable) -> count > 2)
  114. 114. 
 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();
 }); }
  115. 115. ! ! ! 

  116. 116. ! ! ! ♥ ♥ ♥ 

  117. 117. ! ! ! ♥ ♥ ♥
  118. 118. ! ! ! ♥ ♥ ♥
  119. 119. searchRecipe(“ステーキ”) .flatMap(recipes -> fetchLikedRecipeIds(recipes)) .subscribe(recipeIds -> {})
  120. 120. searchRecipe(“ステーキ”) .flatMap(recipes -> fetchLikedRecipeIds(recipes)) .subscribe(recipeIds -> {})
  121. 121. searchRecipe(“ステーキ”) .flatMap(recipes -> fetchLikedRecipeIds(recipes)) .subscribe(recipeIds -> {})
  122. 122. searchRecipe(“ステーキ”) .flatMap(recipes -> fetchLikedRecipeIds(recipes)) .subscribe(recipeIds -> {})
  123. 123. searchRecipe(“ステーキ”)
 .flatMap(recipes -> 
 Observable.combineLatest( Observable.just(recipes), // レシピリストをそのまま渡す
 fetchLikedRecipe(recipes)), //レシピリストからLikeしたレシピIdを取得 Pair::create
 )
 )
 .map(this::mergeLikeState) // mapでLike状態をレシピインスタンスに反映させる(省略) .subscribe(recipes -> {})
  124. 124. searchRecipe(“ステーキ”)
 .flatMap(recipes -> 
 Observable.combineLatest( Observable.just(recipes), // レシピリストをそのまま渡す
 fetchLikedRecipe(recipes)), //レシピリストからLikeしたレシピIdを取得 Pair::create
 )
 )
 .map(this::mergeLikeState) // mapでLike状態をレシピインスタンスに反映させる(省略) .subscribe(recipes -> {})
  125. 125. searchRecipe(“ステーキ”)
 .flatMap(recipes -> 
 Observable.combineLatest( Observable.just(recipes), // レシピリストをそのまま渡す
 fetchLikedRecipe(recipes)), //レシピリストからLikeしたレシピIdを取得 Pair::create
 )
 )
 .map(this::mergeLikeState) // mapでLike状態をレシピインスタンスに反映させる(省略) .subscribe(recipes -> {})
  126. 126. searchRecipe(“ステーキ”)
 .flatMap(recipes -> {
 fetchLikedRecipe(recipes)
 .map(likeIds -> {
 mergeLikeState(recipes, likeIds);
 });
 }
 ) .subscribe(recipes -> {})
  127. 127. public Observable<List<Recipe>> searchRecipeWithLikeState(String keyword) { searchRecipe(keyword) .flatMap(recipes -> Observable.combineLatest( Observable.just(recipes), fetchLikedRecipeIds(recipes)), Pair::create ) ) .map(this::mergeLikeState) }
 public Func1<List<Recipe>> mergeLikeState(Pair<List<Recipe>, Integer> pair) {
 // Like状態をレシピインスタンスに反映させる(省略)
 }
  128. 128. ! ! ! ♥ ♥ ♥
  129. 129. ! ! ! ♥ ♥ ♥
  130. 130. ! ! ! ♥ ♥ ♥
  131. 131. ! ! ! ♥ ♥ ♥
  132. 132. ! ! ! ♥ ♥ ♥
  133. 133. ! ! ! ♥ ♥ ♥
  134. 134. ! ! ! ♥ ♥ ♥
  135. 135. ! ! ! ♥ ♥ ♥
  136. 136. ! ! ! ♥ ♥ ♥
  137. 137. ! ! ! ♥ ♥ ♥
  138. 138. ! ! ! ♥ ♥ ♥
  139. 139. ! ! ! ♥ ♥ ♥
  140. 140. ! ! ! ♥ ♥ ♥
  141. 141. Observable.combineLatest( 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); } ); ! ! ! ♥ ♥ ♥
  142. 142. Observable.combineLatest(
 Observable.just(1),
 Observable.just(2),
 (i1,i2) -> i1 + i2
 ); 

  143. 143. searchRecipe(“ステーキ”) .flatMap(recipes -> recipeClient.fetchLikedRecipeIds(recipes)) .subscribe(recipeIds -> {})
  144. 144. 
 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);
 }
 }
  145. 145. // in your CustomApplication public void onCreate(Bundle saveInstanceState){
 super.onCreate(); System.setProperty("rx.ring-buffer.size", "32"); }
  146. 146. Single.create(subscriber -> {
 subscriber.onSuccess("Hello world");
 }).subscribe(text -> {
 System.out.println(text);
 }, throwable -> {
 Crashlytics.logException(e);
 });
  147. 147. 
 Completable.create(subscriber -> { 
 subscriber.onCompleted()
 }).subscribe(throwable -> {
 Crashlytics.logException(e);
 }, () -> {
 
 });
  148. 148.

×