Last active
February 1, 2017 11:47
-
-
Save amitshekhariitbhu/3a899ff6e73554a361a6b3b36f7518ca to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* First of all we are getting my friends list from | |
* server, then by using flatMap we are emitting users | |
* one by one and then after applying filter we are | |
* returning only those who are following me one by one. | |
*/ | |
/* | |
* This observable return the list of User who are my friends | |
*/ | |
private Observable<List<User>> getAllMyFriendsObservable() { | |
return RxAndroidNetworking.get("https://fierce-cove-29863.herokuapp.com/getAllFriends/{userId}") | |
.addPathParameter("userId", "1") | |
.build() | |
.getObjectListObservable(User.class); | |
} | |
/* | |
* This method does all | |
*/ | |
public void flatMapAndFilter() { | |
getAllMyFriendsObservable() | |
.flatMap(new Func1<List<User>, Observable<User>>() { // flatMap - to return users one by one | |
@Override | |
public Observable<User> call(List<User> usersList) { | |
return Observable.from(usersList); // returning(emitting) user one by one from usersList. | |
} | |
}) | |
.filter(new Func1<User, Boolean>() { // filter operator | |
@Override | |
public Boolean call(User user) { | |
// filtering user who follows me. | |
return user.isFollowing; | |
} | |
}) | |
.subscribeOn(Schedulers.io()) | |
.observeOn(AndroidSchedulers.mainThread()) | |
.subscribe(new Observer<User>() { | |
@Override | |
public void onCompleted() { | |
// do anything onComplete | |
} | |
@Override | |
public void onError(Throwable e) { | |
// handle error | |
} | |
@Override | |
public void onNext(User user) { | |
// only the user who is following me comes here one by one | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment