RxJava2 toList()永远不会发出

时间:2017-11-16 10:21:03

标签: android rx-java2 android-room

所以我跟随Disposable不起作用。我使用Room来从表中获取所有行作为列表,将每个行映射到某个东西并创建一个列表然后它不会从那里继续。

storedSuggestionDao
    .getSuggestionsOrderByType() //Flowable
    .doOnNext(storedSuggestions -> Timber.e("storedSuggestions: " + storedSuggestions)) //this work
    .flatMapIterable(storedSuggestions -> storedSuggestions)
    .map(Selection::create) ))
    .doOnNext(selection -> Timber.e("Selection: " + selection)) // works
    .toList()
    .toObservable() // nothing works after this...
    .doOnNext(selections -> Timber.d("selections: " + selections))
    .map(SuggestionUiModel::create)
    .doOnNext(suggestionUiModel -> Timber.d("suggestionUiModel: " + suggestionUiModel))
    .subscribe();

3 个答案:

答案 0 :(得分:4)

来自第三方的这些类型的数据源通常是无限来源,但toList()需要有限的来源。我想你想要处理storedSuggestions的集合并将它保持在一起。您可以通过内部转型来实现这一目标:

storedSuggestionDao
.getSuggestionsOrderByType() //Flowable
.doOnNext(storedSuggestions -> Timber.e("storedSuggestions: " + storedSuggestions)) //this work
// -------------------------------------
.flatMapSingle(storedSuggestions -> 
    Flowable.fromIterable(storedSuggestions)
    .map(Selection::create)
    .doOnNext(selection -> Timber.e("Selection: " + selection))
    .toList()
)
// -------------------------------------
.doOnNext(selections -> Timber.d("selections: " + selections))
.map(SuggestionUiModel::create)
.doOnNext(suggestionUiModel -> Timber.d("suggestionUiModel: " + suggestionUiModel))
.subscribe();

答案 1 :(得分:0)

我认为在你的情况下你不需要调用.toObserable()

应该是这样的

storedSuggestionDao
.getSuggestionsOrderByType() //Flowable
.doOnNext(storedSuggestions -> Timber.e("storedSuggestions: " + storedSuggestions)) //this work
.flatMapIterable(storedSuggestions -> storedSuggestions)
.map(Selection::create) ))
.doOnNext(selection -> Timber.e("Selection: " + selection)) // works
.toList() // you don't have to call .toObserable()
.map(SuggestionUiModel::create)
.subscribe();

答案 2 :(得分:0)

问题是 storedSuggestionDao.getSuggestionsOrderByType() //Flowable

是热流。 toList仍在等待上游完成