androidrx-javaiterableflatmapconcatmap

RxJava Flowable<List<T>> do operation on each item and then do operation on resulted list


I have a Flowable<List<Item>> for which I want to :

  1. Make an operation on each item
  2. After that, I want to make an operation on the whole resulted list.

This is not working now because it looks like is entering in an infinite loop::

        return list.concatMap(list ->
                    Observable.fromIterable(list)
                            .map(item -> {
                                item.setValue(value);
                                return item;
                            }).toList().toFlowable()
                            .flatMap(updatedList -> mLocalDataSource.update(updatedList)))
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread());

Solution

  • I don't think there's any advantage over just doing this work in the flatMap()/concatMap(). All I'd say is that it would be cleaner if Item were immutable.

    list.concatMap(items -> {
      for(Item item : items)
        item.setValue(value);
      return mLocalDataSource.update(items)
    });