如何在另一个中使用一个可观察值?

How to use one observable values in another?

我有一个 android 应用程序,它从网络中获取 2 种数据类型,一种用于频道列表,另一种用于 epg

重点是,我的 epg url 必须包含频道列表中的频道 ID。这就是我正在尝试做的事情

Observable<ChannelList> channelList =
        NetworkService.getChannels();

List<String> channelIds = new ArrayList<>();

channelList.subscribe(chList -> {
            for (ChannelInfo chInfo : chList.getChannelInfoList()) {
                channelIds.add(chInfo.getId());
            }
        });

Observable<EpgList> epgList =
        NetworkService.getEpgList(channelIds);

但是在方法完成之前它不会获取 ID,那么我如何在此时阻止执行并需要 ID?

试试这个:

NetworkService.getChannels()
                        .map(chList -> chList.getChannelInfoList())
                        .flatMapIterable(list -> list)
                        .map(channelInfo -> channelInfo.getId())
                        .toList()
                        .switchMap(channelIds -> NetworkService.getEpgList(channelIds))
                        .subscribe(epgList -> Timber.d("%s", epgList));