SlideShare a Scribd company logo
明日から使えるRxjava頻出パターン (Droid kaigi 2016)
明日から使えるRxjava頻出パターン (Droid kaigi 2016)
明日から使えるRxjava頻出パターン (Droid kaigi 2016)
明日から使えるRxjava頻出パターン (Droid kaigi 2016)








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










public interface Callback {
void onSuccess(List<Recipe> recipes);
void onError(Throwable t);
}
public void searchRecipe(String keyword, Callback callback) {

recipeApiClient.search(keyword, callback);

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

addToList(recipes);
}
@Override
public void onError(Throwable t) {
showErrorView(t);
}
});

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

addToList(recipes);
}
@Override
public void onError(Throwable t) {
showErrorView(t);
}
});

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

addToList(recipes);
}
@Override
public void onError(Throwable t) {
showErrorView(t);
}
});

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

addToList(recipes);
}
@Override
public void onError(Throwable t) {
showErrorView(t);
}
});

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

addToList(recipes);
}
@Override
public void onError(Throwable t) {
showErrorView(t);
}
});

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

addToList(recipes);
}
@Override
public void onError(Throwable t) {
showErrorView(t);
}
});

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

addToList(recipes);
}
@Override
public void onError(Throwable t) {
showErrorView(t);
}
});

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

addToList(recipes);
}
@Override
public void onError(Throwable t) {
showErrorView(t);
}
});

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

addToList(recipes);
}
@Override
public void onError(Throwable t) {
showErrorView(t);
}
});

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

addToList(recipes);
}
@Override
public void onError(Throwable t) {
showErrorView(t);
}
});

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

addToList(recipes);
}
@Override
public void onError(Throwable t) {
showErrorView(t);
}
});

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

addToList(recipes);
}
@Override
public void onError(Throwable t) {
showErrorView(t);
}
});

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

addToList(recipes);
}
@Override
public void onError(Throwable t) {
showErrorView(t);
}
});

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


# 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 onSuccess(List<Recipe> recipes) {
}
@Override
public void onError(Throwable t) {
}
});

}


public void searchRecipe(String keyword) {

recipeApiClient.search(keyword, new Callback() {
@Override
public void onSuccess(List<Recipe> recipes) {
}
@Override
public void onError(Throwable t) {
}
});

}


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) {

}
});

}


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) {

}
});

}
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) {

}

});
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
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
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
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
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);

}
}
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() { 

});
);
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() { 

});
);
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() { 

});
);
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() { 

});
);
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() { 

});
);
!
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() { 

});
);
!
!
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() { 

});
);
!
!
!
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() { 

});
);
!
!
!
!
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);
},
() -> {

// 省略可能

}
);
!
!
!
!
🙇
明日から使えるRxjava頻出パターン (Droid kaigi 2016)
明日から使えるRxjava頻出パターン (Droid kaigi 2016)
明日から使えるRxjava頻出パターン (Droid kaigi 2016)
明日から使えるRxjava頻出パターン (Droid kaigi 2016)
明日から使えるRxjava頻出パターン (Droid kaigi 2016)
明日から使えるRxjava頻出パターン (Droid kaigi 2016)
searchRecipeBy(“ステーキ”)

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

.subscribeOn(Schedulers.io())

.map(recipes -> { /* todo something */ })
.subscribe(recipes -> {});
searchRecipeBy(“ステーキ”)

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

.subscribeOn(Schedulers.io())

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


searchRecipeBy(“ステーキ”)

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

.subscribeOn(Schedulers.io())

.observeOn(AndroidSchedulers.mainThread())

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

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

.subscribeOn(Schedulers.io())

.observeOn(AndroidSchedulers.mainThread())

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

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

.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(recipes -> {});


searchRecipe(“ステーキ”)

.subscribeOn(schedulers.io())

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

.subscribeOn(schedulers.io())

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

.subscribeOn(schedulers.io())

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

.subscribeOn(schedulers.io())

.observeOn(AndroidSchedulers.mainThread())
.subscribe(recipes -> {
addToList(recipes);
},
throwable -> {
showErrorMessage(t);
}
);
!
!
!
!
明日から使えるRxjava頻出パターン (Droid kaigi 2016)


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);
}
});

}


# app/build.gradle

dependencies {

compile 'com.trello:rxlifecycle:0.4.0'
compile 'com.trello:rxlifecycle-components:0.4.0'

}
searchRecipeBy(“ハンバーグ”)
.compose(bindToLifecycle())

.subscribeOn(schedulers.io())

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

.subscribeOn(schedulers.io())

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

.subscribeOn(schedulers.io())

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

.subscribeOn(schedulers.io())

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

.subscribeOn(schedulers.io())

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

.subscribeOn(schedulers.io())

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

.subscribeOn(schedulers.io())

.observeOn(AndroidSchedulers.mainThread())
.subscribe(recipes -> {
addList(recipes);
},
throwable -> {
showErrorMessage(t);
}
);


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

.subscribeOn(schedulers.io())

.observeOn(AndroidSchedulers.mainThread())
.subscribe(recipes -> {
addList(recipes);
},
throwable -> {
showErrorMessage(t);
}
);




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


#
#
#
#
!
!
!
#
#
#
#
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
明日から使えるRxjava頻出パターン (Droid kaigi 2016)
!
!
!
!
!
!
!
!
!
!
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.combineLatest(

Observable.just(1),

Observable.just(2),

Pair::create

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

.subscribeOn(schedulers.io())

.observeOn(AndroidSchedulers.mainThread())
.subscribe(pair -> {
addToListWithBannerIfExist(pair.first, pair.second);
},
throwable -> {
showErrorMessage(t);
}
);
Observable.combineLatest(
searchRecipe(“ステーキ”),
searchAd(“ステーキ”), // 広告情報を取得するメソッドだと思って下さい
Pair::create
)

.subscribeOn(schedulers.io())

.observeOn(AndroidSchedulers.mainThread())
.subscribe(pair -> {
addToListWithBannerIfExist(pair.first, pair.second);
},
throwable -> {
showErrorMessage(t);
}
);
Observable.combineLatest(
searchRecipe(“ステーキ”),
searchAd(“ステーキ”), // 広告情報を取得するメソッドだと思って下さい
Pair::create
)

.subscribeOn(schedulers.io())

.observeOn(AndroidSchedulers.mainThread())
.subscribe(pair -> {
addToListWithBannerIfExist(pair.first, pair.second);
},
throwable -> {
showErrorMessage(t);
}
);
Observable.combineLatest(
searchRecipe(“ステーキ”),
searchAd(“ステーキ”), // 広告情報を取得するメソッドだと思って下さい
Pair::create
)

.subscribeOn(schedulers.io())

.observeOn(AndroidSchedulers.mainThread())
.subscribe(pair -> {
addToListWithBannerIfExist(pair.first, pair.second);
},
throwable -> {
showErrorMessage(t);
}
);
Observable.combineLatest(
searchRecipe(“ステーキ”),
searchAd(“ステーキ”), // 広告情報を取得するメソッドだと思って下さい
Pair::create
)

.subscribeOn(schedulers.io())

.observeOn(AndroidSchedulers.mainThread())
.subscribe(pair -> {
addToListWithBannerIfExist(pair.first, pair.second);
},
throwable -> {
showErrorMessage(t);
}
);
Observable.combineLatest(
searchRecipe(“ステーキ”),
searchAd(“ステーキ”), // 広告情報を取得するメソッドだと思って下さい
Pair::create
)

.subscribeOn(schedulers.io())

.observeOn(AndroidSchedulers.mainThread())
.subscribe(pair -> {
addToListWithBannerIfExist(pair.first, pair.second);
},
throwable -> {
showErrorMessage(t);
}
);
Observable.combineLatest(
searchRecipe(“ステーキ”),
searchAd(“ステーキ”), // 広告情報を取得するメソッドだと思って下さい
Pair::create
)

.subscribeOn(schedulers.io())

.observeOn(AndroidSchedulers.mainThread())
.subscribe(pair -> {
addToListWithBannerIfExist(pair.first, pair.second);
},
throwable -> {
showErrorMessage(t);
}
);
Observable.combineLatest(
searchRecipe(“ステーキ”),
searchAd(“ステーキ”), // 広告情報を取得するメソッドだと思って下さい
Pair::create
)

.subscribeOn(schedulers.io())

.observeOn(AndroidSchedulers.mainThread())
.subscribe(pair -> {
addToListWithBannerIfExist(pair.first, pair.second);
},
throwable -> {
showErrorMessage(t);
}
);
明日から使えるRxjava頻出パターン (Droid kaigi 2016)
明日から使えるRxjava頻出パターン (Droid kaigi 2016)
明日から使えるRxjava頻出パターン (Droid kaigi 2016)
明日から使えるRxjava頻出パターン (Droid kaigi 2016)
😱
😱
😱
AdObservable(“タマゴ”)
.onErrorReturn(throwable -> AdModel.EmptyData());
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();

});
}
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();

});
}
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();

});
}
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();

});
}
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();

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

.subscribeOn(schedulers.io())

.observeOn(AndroidSchedulers.mainThread())
.subscribe( /* 省略 */ );
Observable.combineLatest(
searchRecipe(“ステーキ”),
searchAd(“ステーキ”),
Pair::create
)

.subscribeOn(schedulers.io())

.observeOn(AndroidSchedulers.mainThread())
.subscribe( /* 省略 */ );
Observable.combineLatest(
searchRecipe(“ステーキ”),
searchAd(“ステーキ”),
Pair::create
)

.subscribeOn(schedulers.io())

.observeOn(AndroidSchedulers.mainThread())
.subscribe( /* 省略 */ );
Observable.combineLatest(
searchRecipe(“ステーキ”),
searchAd(“ステーキ”),
Pair::create
)

.subscribeOn(schedulers.io())

.observeOn(AndroidSchedulers.mainThread())
.subscribe( /* 省略 */ );
Observable.combineLatest(
searchRecipe(“ステーキ”),
searchAd(“ステーキ”),
Pair::create
)

.subscribeOn(schedulers.io())

.observeOn(AndroidSchedulers.mainThread())
.subscribe( /* 省略 */ );
Observable.combineLatest(
searchRecipe(“ステーキ”).subscribeOn(schedulers.io()),
searchAd(“ステーキ”).subscribeOn(schedulers.io()),
Pair::create
)

.subscribeOn(schedulers.io())

.observeOn(AndroidSchedulers.mainThread())
.subscribe( /* 省略 */ );


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


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();

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


!
!
!
♥
♥
♥


!
!
!
♥
♥
♥
!
!
!
♥
♥
♥
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), // レシピリストをそのまま渡す

fetchLikedRecipe(recipes)), //レシピリストからLikeしたレシピIdを取得
Pair::create

)

)

.map(this::mergeLikeState) // mapでLike状態をレシピインスタンスに反映させる(省略)
.subscribe(recipes -> {})
searchRecipe(“ステーキ”)

.flatMap(recipes -> 

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

fetchLikedRecipe(recipes)), //レシピリストからLikeしたレシピIdを取得
Pair::create

)

)

.map(this::mergeLikeState) // mapでLike状態をレシピインスタンスに反映させる(省略)
.subscribe(recipes -> {})
searchRecipe(“ステーキ”)

.flatMap(recipes -> 

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

fetchLikedRecipe(recipes)), //レシピリストからLikeしたレシピIdを取得
Pair::create

)

)

.map(this::mergeLikeState) // mapでLike状態をレシピインスタンスに反映させる(省略)
.subscribe(recipes -> {})
searchRecipe(“ステーキ”)

.flatMap(recipes -> {

fetchLikedRecipe(recipes)

.map(likeIds -> {

mergeLikeState(recipes, likeIds);

});

}

)
.subscribe(recipes -> {})
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状態をレシピインスタンスに反映させる(省略)

}
明日から使えるRxjava頻出パターン (Droid kaigi 2016)
!
!
!
♥
♥
♥
!
!
!
♥
♥
♥
!
!
!
♥
♥
♥
!
!
!
♥
♥
♥
!
!
!
♥
♥
♥
!
!
!
♥
♥
♥
!
!
!
♥
♥
♥
!
!
!
♥
♥
♥
!
!
!
♥
♥
♥
!
!
!
♥
♥
♥
!
!
!
♥
♥
♥
!
!
!
♥
♥
♥
!
!
!
♥
♥
♥
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);
}
);
!
!
!
♥
♥
♥
明日から使えるRxjava頻出パターン (Droid kaigi 2016)
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 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);

}

}
// in your CustomApplication
public void onCreate(Bundle saveInstanceState){

super.onCreate();
System.setProperty("rx.ring-buffer.size", "32");
}
明日から使えるRxjava頻出パターン (Droid kaigi 2016)
Single.create(subscriber -> {

subscriber.onSuccess("Hello world");

}).subscribe(text -> {

System.out.println(text);

}, throwable -> {

Crashlytics.logException(e);

});


Completable.create(subscriber -> { 

subscriber.onCompleted()

}).subscribe(throwable -> {

Crashlytics.logException(e);

}, () -> {



});



More Related Content

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