Skip to content

Instantly share code, notes, and snippets.

@amitshekhariitbhu
Last active February 1, 2017 11:46
Show Gist options
  • Save amitshekhariitbhu/95664357a7aa85e96d13587e60cf843e to your computer and use it in GitHub Desktop.
Save amitshekhariitbhu/95664357a7aa85e96d13587e60cf843e to your computer and use it in GitHub Desktop.
/*
* This observable return the list of User who loves cricket
*/
private Observable<List<User>> getCricketFansObservable() {
return RxAndroidNetworking.get("https://fierce-cove-29863.herokuapp.com/getAllCricketFans")
.build()
.getObjectListObservable(User.class);
}
/*
* This observable return the list of User who loves Football
*/
private Observable<List<User>> getFootballFansObservable() {
return RxAndroidNetworking.get("https://fierce-cove-29863.herokuapp.com/getAllFootballFans")
.build()
.getObjectListObservable(User.class);
}
/*
* This do the complete magic, make both network call
* and then returns the list of user who loves both
* Using zip operator to get both response at a time
*/
private void findUsersWhoLovesBoth() {
// here we are using zip operator to combine both request
Observable.zip(getCricketFansObservable(), getFootballFansObservable(),
new Func2<List<User>, List<User>, List<User>>() {
@Override
public List<User> call(List<User> cricketFans,
List<User> footballFans) {
List<User> userWhoLovesBoth =
filterUserWhoLovesBoth(cricketFans, footballFans);
return userWhoLovesBoth;
}
}
).subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<List<User>>() {
@Override
public void onCompleted() {
// do anything onComplete
}
@Override
public void onError(Throwable e) {
// handle error
}
@Override
public void onNext(List<User> users) {
// do anything with user who loves both
}
});
}
private List<User> filterUserWhoLovesBoth(List<User> cricketFans, List<User> footballFans) {
List<User> userWhoLovesBoth = new ArrayList<>();
// your logic to filter who loves both
return userWhoLovesBoth;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment