函数不在Rxjava中返回数据

时间:2018-04-11 08:20:35

标签: android rx-java rx-java2

我在rxjava中调用函数以获取热门故事:

public Observable<Integer> getStories(final Story.FILTER FILTER) {
    return api.getStories(FILTER)
            .flatMap(new Func1<List<ContentValues>, Observable<Integer>>() {
                @Override
                public Observable<Integer> call(final List<ContentValues> stories) {
                    return Observable.create(new Observable.OnSubscribe<Integer>() {
                        @Override
                        public void call(Subscriber<? super Integer> subscriber) {
                            Log.e("Total size",""+stories.size());
                            refreshPreferences.saveRefreshTick(FILTER);
                            dataPersister.persistStories(stories);
                            subscriber.onNext(stories.size());
                            subscriber.onCompleted();
                        }
                    });
                }
            });
}

进一步调用以下函数获取所有故事:

public Observable<List<ContentValues>> getStories(final Story.FILTER FILTER) {

    return hackerNewsService.getTopStories()
            .flatMap(new Func1<List<Long>, Observable<Long>>() {
        @Override
        public Observable<Long> call(final List<Long> dataSnapshot) {
            return Observable.create(new Observable.OnSubscribe<Long>() {
                @Override
                public void call(Subscriber<? super Long> subscriber) {
                    for (int i = 0; i < dataSnapshot.size(); i++) {
                        subscriber.onNext(dataSnapshot.get(i));
                    }
                    subscriber.onCompleted();
                }
            });
        }
    }).flatMap(new Func1<Long, Observable<Post>>() {
                @Override
                public Observable<Post> call(final Long storyRoot) {
                    return hackerNewsService.getStoryItem(String.valueOf(storyRoot));
                }
            }).flatMap(new Func1<Post, Observable<ContentValues>>() {
        @Override
        public Observable<ContentValues> call(final Post story) {
            return Observable.create(new Observable.OnSubscribe<ContentValues>() {
                @Override
                public void call(Subscriber<? super ContentValues> subscriber) {
                    Log.e("StoryTitle",""+story.getTitle());
                    ContentValues contentValues = mapStory(story);

                    if (story != null) {
                        subscriber.onNext(contentValues);
                    } else {
                        subscriber.onNext(new ContentValues());
                        //Inject.crashAnalytics().logSomethingWentWrong("HNewsApi: onDataChange is empty in " + story.getTitle());
                    }
                }
            });
        }
    }).toList();
}

我使用flatmap将数据从一种形式转换为另一种形式。现在,当我运行代码时,我没有得到数据返回到我的第一个函数,我在持久存储中保存数据。第二个功能是返回正确的数据。

我不知道我在这里做了什么错。我是rxjava的新手,所以我将不胜感激。

1 个答案:

答案 0 :(得分:0)

首先,请勿使用Observable.create(),而是使用更合适的mapflatMapIterable运营商。这样,Observable协议在整个流程中都得到了尊重:

public Observable<Integer> getStories(final Story.FILTER FILTER) {
    return api.getStories(FILTER)
        .map(new Func1<List<ContentValues>, Integer>() {
            @Override
            public Integer call(final List<ContentValues> stories) {
                 Log.e("Total size",""+stories.size());
                 refreshPreferences.saveRefreshTick(FILTER);
                 dataPersister.persistStories(stories);
                 return stories.size();
            }
        });
}

public Observable<List<ContentValues>> getStories(final Story.FILTER FILTER) {

    return hackerNewsService.getTopStories()
        .flatMapIterable(new Func1<List<Long>, List<Long>>() {
            @Override
            public List<Long> call(final List<Long> dataSnapshot) {
                return dataSnapshot;
            }
        })
        .flatMap(new Func1<Long, Observable<Post>>() {
            @Override
            public Observable<Post> call(final Long storyRoot) {
                return hackerNewsService.getStoryItem(String.valueOf(storyRoot));
            }
        })
        .map(new Func1<Post, ContentValues>() {
            @Override
            public ContentValues call(final Post story) {
                Log.e("StoryTitle",""+story.getTitle());
                ContentValues contentValues = mapStory(story);

                if (story != null) {
                    return contentValues;
                }
                return new ContentValues();
        })
        .toList();
}