Hey i am new to RxJava and try want to achieve the following: I have the following methods:
Flowable<List<Group>> getGroups();
Flowable<List<User>> getMembersForGroup(String groupid);
Now actually i want to get a result looking like:
Group g1 -> List members1
Group g2-> List members2 ....
Speaking: knowing the members of a group for every group.
I tried it with something like
getGroups().flatMap( //map getMembers(gid) for each gid )
but i get stuck at the point "for each gid", because i dont know excactly what i actually want to return from flatMaps Func1 and how to handle it in flatMaps Func2.
Can somebody help?
Unfortunately i still have no idea, im not that much into the thinking of rxjava. Here is what i have got so far:
model.getGroupData()
.flatMap(new Func1<QueryDocumentSnapshot,Flowable<Maybe<List<UserSchema>>>>() {
@Override
public Flowable<Maybe<List<UserSchema>>> call(QueryDocumentSnapshot doc) {
Groups groups = doc.toObject(Groups.class);
List<Maybe<List<UserSchema>>> memberflow = new ArrayList<>();
for (GroupSchema g: groups){
memberflow.add(model.getMembersOfGroup(g.getId()));
}
return Flowable.fromIterable(memberflow);
}
},
new Func2<QueryDocumentSnapshot, Flowable<Maybe<List<UserSchema>>>,Flowable>() {
@Override
public Flowable call(QueryDocumentSnapshot queryDocumentSnapshot, Flowable<Maybe<List<UserSchema>>> flowable) {
//zip?
}
});
As you can see i actually have a QueryDocumentSnapshot which does not really matter, cause it can be easily converted to the mentioned list.
So i am still not sure what i shall return from Func1, and hence what to zip.
This is one approach you could use (there are others, including the zip
one):
groupApi
.getGroups()
.flatMap(Flowable::fromIterable)
.map(Group::getId)
.distinct()
.flatMap(groupId -> groupApi.getMembersForGroup(groupId).map(users -> new Pair(groupId, users)))
.subscribe(System.out::println);