编程语言
首页 > 编程语言> > android-RxJava2 toList()从不发出

android-RxJava2 toList()从不发出

作者:互联网

所以我有下面的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();

解决方法:

来自第三方的这些类型的数据源通常是无限来源,但是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();

标签:rx-java2,android-room,android
来源: https://codeday.me/bug/20191025/1929482.html