RxJava2 toList()从不发出 [英] RxJava2 toList() never emits

查看:86
本文介绍了RxJava2 toList()从不发出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我发现以下Disposable无效.我正在使用Room从一个表中获取所有行作为列表,将它们中的每行映射到某个东西并创建一个列表,然后它不会从那里继续.

So I have following Disposable which doesn't work. I am using Room to get all rows from a table as a list, map each of them to something and create a list and then it doesn't continue from there.

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 的那个集合并将其放在一起.您可以通过内部转换来实现:

These types of data sources from 3rd parties are usually infinite sources but toList() requires a finite source. I guess you wanted to process that collection of storedSuggestions and keep it together. You can achieve this via an inner transformation:

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();

这篇关于RxJava2 toList()从不发出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆