Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using RxJava to fetch object, transform a containing list, and use the list

The high level problem I'm trying to solve is transforming a list of Foo objects contained in a fetched FooContainer (Observable) to a list of FooBar objects using RxJava.

My (confused) attempt:

fooContainerObservable
  .map(container -> container.getFooList())
  .flatMap(foo -> transformFooToFooBar(foo))
  .collect( /* What do I do here? Is collect the correct thing? Should I be using lift? */)
  .subscribe(fooBarList -> /* Display the list */);

My confusion (or at least one point of it) is the step moving the flattened list back to a list.

Note: I'm attempting to do this in an Android app.

like image 869
loeschg Avatar asked Dec 08 '14 16:12

loeschg


1 Answers

toList can collect all items of an Observable into a List and emit it. toList can be implemented using collect, but it's much easier than collect. For your example, it will be:

fooContainerObservable
  .map(container -> container.getFooList())
  .flatMap(foo -> transformFooToFooBar(foo))
  .toList()
  .subscribe(fooBarList -> /* Display the list */);
like image 144
zsxwing Avatar answered Oct 11 '22 14:10

zsxwing