【发布时间】: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