【问题标题】:how to map values returned from observable to be used in another observable如何映射从可观察对象返回的值以用于另一个可观察对象
【发布时间】:2016-01-03 03:25:24
【问题描述】:

我有以下 observable 从数据库获取提要 ID 列表(我使用糖 ORM 库

  public Observable<Set<Long>> getFeedIdsFromDB() {
            return Observable.create(subscriber -> {

                Set<Integer> subscribedFeedIds = new HashSet<>();

               //get feed ids from FeedEntity Table

                for (FeedEntity feed : FeedEntity.listAll(FeedEntity.class)){
                    if (feed.isSubscribed()){
                        subscribedFeedIds.add(feed.getFeedId());
                    }
                }
            });
    }

这个 Observable 应该发出用于 api 调用的 id:

public Observable<StoryCollectionEntity> storyEntityList(final int page) {
        return this.restApi.storyCollection(/* this is feed ids*/ id, page)
                .distinct(storyCollectionEntity -> storyCollectionEntity)
                .doOnNext(saveStoryCollectionToCacheAction)

}

我想我应该使用某种映射,但不知道如何实现它。

编辑: 我做了以下修改:

// To map feed ids (retrieved from database) to getAllStoryEntityList Observable: 
@Override
    public Observable<StoryCollectionEntity> storyEntityList(final int page) {
        return this.mNewsCache.getFeedIdsFromDB().flatMap(id -> getAllStoryEntityList(page, id));
    }



//call restApi
    public Observable<StoryCollectionEntity> getAllStoryEntityList(final int page, Set<Long> id){
            return this.restApi.storyCollection( id, page)
                    .distinct(storyCollectionEntity -> storyCollectionEntity)
                    .doOnNext(saveStoryCollectionToCacheAction);
        }

但永远不会调用 api 服务。映射中有问题。

@GET("story")
     Observable<StoryCollectionEntity> storyCollection(
            @Query("feed_ids") Set<Long> feedIds,
            @Query("page") int page);

【问题讨论】:

    标签: android rx-java rx-android


    【解决方案1】:

    getFeedIdsFromDB 中创建的 Observable 不会发出任何项目,因此您的 flatMap 和其他数据转换永远不会发生,因为流实际上没有 数据。您可以通过直接订阅返回的 Observable 并为 onNext 做一些事情来测试这一点。

    getFeedIdsFromDB().subscribe(feedId -> System.out.println(feedId));
    

    您应该看到没有打印任何内容。使用Observable#create 时,必须手动调用匿名类中subscriberonNext 方法,并使用您希望向下游传递的任何数据。文档为此提供了示例代码。

    所以修改你的 Observable 来调用 onNext,我们得到这个:

    public Observable<Set<Long>> getFeedIdsFromDB() {
        return Observable.create(subscriber -> {
            Set<Integer> feedIds = new HashSet<>();
    
            // get feed ids from FeedEntity Table
            for (FeedEntity feed : FeedEntity.listAll(FeedEntity.class)){
                feedIds.add(feed.getFeedId());
            }
    
            // emit a single Set and complete
            if (subscriber.isSubscribed()) {
                subscriber.onNext(feedIds);
                subscriber.onCompleted();
            }
        });
    }
    

    现在应该传递Set。如果您的目标是在转换后最终发射单个 StoryCollectionEntity 对象(如果我没看错的话),那么您的映射看起来是正确的。

    【讨论】:

      【解决方案2】:

      我不确定预期的输出是什么,但我会告诉你一种方法来做这种事情。也许您可以更改它以适合您的用例。

      第一步是允许id作为storyEntityList中的函数参数:

      public Observable<StoryCollectionEntity> storyEntityList(final int page, int id) {
              return this.restApi.storyCollection(/* this is feed ids*/ id, page)
                      .distinct(storyCollectionEntity -> storyCollectionEntity)
                      .doOnNext(saveStoryCollectionToCacheAction)
      

      现在您可以使用Observable.flatMap

      public Observable<StoryCollectionEntity> getAllStoryEntityList(int page){
          return getFeedIdsFromDB().flatMap(id -> storyEntityList(page, id));
      }
      

      命名可能不正确,但同样 - 我不确定实体是什么。

      【讨论】:

      • 我已听从您的建议,但从未调用 getStoryEntityList
      • 这暗示没有从数据库返回 ids(空集)。不是这样吗?
      • 实际上它返回值 ```` public Observable> getFeedIdsFromDB() { return Observable.create(subscriber -> { Set subscribedFeedIds = new HashSet() ; for (FeedEntity feed : FeedEntity.listAll(FeedEntity.class)){ if (feed.isSubscribed()){ subscribedFeedIds.add(feed.getFeedId()); } } }); } ````
      猜你喜欢
      • 2019-02-18
      • 2017-07-12
      • 2021-09-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-16
      • 1970-01-01
      相关资源
      最近更新 更多