Please look at the picture beforehand. Imagine I've got the following methods:
Observable<FootballPlayer> loadPlayersForTeam(int teamId)
for example, Barcelona FC has a teamId = 13213
, so loadPlayersForTeam(13213)
will return an Observable that emits FootballPlayers objects:
FootballPlayer("Messi", 10), FootballPlayer("Suarez", 9), etc
.
This observable will eventually emit every football player of the current squad of Barcelona FC. This corresponds to the regions #1 + #2 at the picture.
And then I've got another methods:
Observable<Coach> loadCoachesForTeam(int teamId)
Single<FootballPlayer> loadPlayer(int playerId)
I hope the names of the object speak for themselves. And here's the thing: there's favoritePlayerId field under class Coach, so effectively every Coach maps to some playerId. For example, Coach('Pep Guardiola') will have .favoritePlayerId = 9 which stands for Messi (note that we could have downloaded FootballPlayer("Messi", 10) from Observable loadPlayersForTeam(int teamId).
And it may happen that Coach('Ronald Koeman').favoritePlayerId = 99
and there's no FootballPlayer
with .favoritePlayerId = 99
under current squad of Barcelona FC (so we won't get it using loadPlayersForTeam()
) and because we're really huge supporters and we have to use loadPlayer(99)
to get that old FootballPlayer
. A region of players that we can download using loadCoachesForTeam()
and then calling loadPlayer() for each emitted coach is region #3.
Here's a question:
FootballPlayer objects are really heavy, that's why we don't want to call loadPlayerSingle(int playerId) for region #2 and only to call for region #3, for example: we don't want to call loadPlayer(9)
– load Messi using this method, cause we've already received him in the observable from loadPlayersForTeam().
What I want is to return a new Observable<FootballPlayer>
– it should emit both current + favorites of the coaches (note: FootballPlayers themselves and not their Ids only) and only call loadPlayer() for the Ids which are not in the current squad, cause these calls are really expensive: if my playerIds
from FootballPlayer objects from loadPlayersForATeam
are 9, 10, 21, 1
and the .favoritePlayerId of loadCoachesForTeam()
are 1, 3, 44, 5, 1
I want to make sure I won't call loadPlayer(1)
: again, because we can get it from the 1st observable (which is loadPlayersForTeam(Barcelona.teamId)
). The ids in the 1st observable is unique, and you can't say so about the 2nd one (loadCoachesForTeam(Barcelona.teamId). So what I tried is to use distinct()
and mergeWith()
for this 2 observables. But distinct()
only operates on the prefix of the events so far and doesn't wait for the observable to comlpete: it may happen that we get 1 from the 2nd observable.
How can I call distinct for the whole Observable and then add there some more values without using Subjects (call loadPlayer()
for region #3 only and take FootballPlayers from #2 from the 1st observable)?
Thanks.