java - RxJava Iterate over list making API call for each item and collecting result into single list -
am facing strange issue, started migrate of codebase reactive new rxjava, using rxjava2 api handling, , below problem statement.
api getgamedetails returns observable<gameresponse> contains information regarding particular game, need poll server every 10 second fetch latest game status, game status response contains list<player> system need query server check weather player guest or registered.
as contains multiple api call, tried chain them in end list<player> updated information, below try unfortunately not working expected, need in debugging out same.
thanks in advance.
disposable d = observable.interval(10, timeunit.seconds) .flatmap(new function<long, observablesource<gameresponse>>() { @override public observablesource<gameresponse> apply(@nonnull long along) throws exception { return gameplaymanager .getgamedetails(mgameid) .doonerror(new consumer<throwable>() { @override public void accept(@nonnull throwable throwable) throws exception { log.d("####", throwable.getmessage(), throwable); } }) .onerrorresumenext(new function<throwable, observablesource<gameresponse>>() { @override public observablesource<gameresponse> apply(@nonnull throwable throwable) throws exception { return observable.empty(); } }); } }) .takeuntil(new predicate<gameresponse>() { @override public boolean test(@nonnull gameresponse gameresponse) throws exception { // check if has reached x players limit if (gameresponse != null && gameresponse.players != null) { if (gameresponse.players.size() >= maximum_number_of_players_in_room) { return true; } } return false; } }) .map(new function<gameresponse, list<player>>() { @override public list<player> apply(@nonnull gameresponse gameresponse) throws exception { gamedetailresponse = gameresponse; return gameresponse.players; } }) .flatmap(new function<list<player>, observablesource<list<player>>>() { @override public observablesource<list<player>> apply(@nonnull list<player> players) throws exception { return observable.fromiterable(players).flatmap(new function<player, observablesource<player>>() { @override public observablesource<player> apply(@nonnull final player player) throws exception { return observable.fromcallable(new callable<player>() { @override public player call() throws exception { userprofile profile = userservice.profile(token, player.playerid); if (profile != null) { player.internaluser = true; } return player; } }); } }) .tolist() .toobservable(); } }) .doonnext(new consumer<list<player>>() { @override public void accept(@nonnull list<player> players) throws exception { // code presenting ui here // } }) .doonerror(new consumer<throwable>() { @override public void accept(@nonnull throwable throwable) throws exception { log.d("####",throwable.getmessage(),throwable); startgameview.showerrormessage(); } }) .subscribe(); subscriptions.add(d);
Comments
Post a Comment