问题
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();
回答1:
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();
回答2:
I think in your case you don't need to call .toObserable()
It should be like this
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();
回答3:
The problem is
storedSuggestionDao.getSuggestionsOrderByType() //Flowable
is a hot stream.
toList
still waits for upstream to complete
来源:https://stackoverflow.com/questions/47327070/rxjava2-tolist-never-emits