0

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());
mantc_sdr
  • 451
  • 3
  • 17
  • Hmm, the code you have here doesn't look like it would enter an infinite loop. You could change a few operators (`flatMapIterable(l -> l)` instead of `concatMap(l -> Observable.fromIterable(...)`, `doOnNext` instead of `map`, `flatMapPublisher` instead of `toFlowable().flatMap(...)`), but what you have there should work. I do agree with @PPartisan, though - just iterating over the list, rather than flattening it/rebuilding it via RxJava operators, is a better choice. – dano Sep 01 '21 at 16:29

1 Answers1

3

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)
});
PPartisan
  • 8,173
  • 4
  • 29
  • 48