RxJava flatMapIterable with one

I'm trying to tidy up my code a bit, and Single seems to be a good choice for me, because I am doing something that will only ever emit one result.

I had a problem, although earlier I used flatMapIterable to answer (list) and did something on each item. I do not see how I can achieve this with Single.

 getListOfItems() .flatMapIterable(items -> items) .flatMap(item -> doSomethingWithItem()) .toList() 

This works fine if getListOfItems returns an Observable , but if I try to return it Single , then I cannot make flatMapIterable and see no alternative, any ideas?

+7
android rx-android rx-java2
source share
2 answers

flattenAsObservable should do the trick, it will display the Single success value in Iterable (list) and emit each element of the list as Observable .

  getListOfItems() .flattenAsObservable(new Function<Object, Iterable<?>>() { @Override public Iterable<?> apply(@NonNull Object o) throws Exception { return toItems(o); } }) .flatMap(item -> doSomethingWithItem()) .toList() 
+18
source share

I did something like this

 @Override public Single<List<String>> getAvailablePaths() { return mongoClient.rxFind("t_polygons", new JsonObject()) .toObservable() .flatMapIterable(list -> list) .map(json -> json.getString("path")) .filter(Objects::nonNull) .toList() .toSingle(); } 
+1
source share

All Articles