Reputation: 85
I have a DAO method like this, which is working fine:
@Query("SELECT name FROM Weather")
Single<List<String>> getCity();
And a method in my activity:
mDatabase.getWeatherDao().getCity()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.flatMap(new Function<List<String>, SingleSource<String>>() {
@Override
public SingleSource<String> apply(List<String> strings) throws Exception {
return ....;
}
})
.distinct()
next filter and so on.
How can I emit each item from List<String>
strings in apply method, so that I can delete repeating (distinct()
) items, filter and then use method toList()
Upvotes: 0
Views: 56
Reputation: 3500
You could try something like this as well:
mDatabase.getWeatherDao().getCity()
.toObservable()
.flatMapIterable(r -> r)
.distinct()
.toList()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(..., ...)
This will work "as is" with strings, for more complex objects you'll need to make sure hashes and equals are working correct (of the entities inside the list).
Upvotes: 0
Reputation: 2852
You have to flatMap
an Observable, like this:
.flatMapObservable(new Function<ArrayList<String>, ObservableSource<? extends String>>() {
@Override
public ObservableSource<? extends String> apply(ArrayList<String> source) throws Exception {
return Observable.fromIterable(source);
}
})
If you can use lambdas and method references you could replace all this ceremony with one of these:
// Lambda version
.flatMapObservable(source -> Observable.fromIterable(source))
// Method reference version
.flatMapObservable(Observable::fromIterable)
Upvotes: 1