RxJava迭代列表,为每个项目调用API,并将结果收集到单个列表中

时间:2017-07-26 08:52:42

标签: java android rx-java rx-java2

我面临着一个奇怪的问题,最近只有我开始将我的大部分代码库迁移到反应中,所以说我对RxJava几乎是新手,我使用RxJava2进行API处理,下面是我的问题陈述。

API getGameDetails返回Observable<GameResponse>,其中包含有关特定游戏的信息,需要每10秒轮询服务器以获取最新游戏状态,游戏状态响应包含List<Player>系统需要查询服务器以检查天气Player是客人还是已注册。

由于它包含多个API调用,我尝试将它们链接在一起,所以最后我会得到List<Player>更新的信息,下面是我的尝试但不幸的是它没有按预期工作,需要你的帮助调试出来相同。

提前致谢。

    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 it has reached X players limit already
                    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 {

                    // Some code for 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);

0 个答案:

没有答案