我正在尝试解决的高级问题是使用RxJava将获取的Foo
(Observable)中包含的FooContainer
个对象的列表转换为FooBar
个对象的列表。
我的(困惑)尝试:
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 */);
我的困惑(或至少有一点)是将展平列表移回列表的步骤。
注意:我正在尝试在Android应用中执行此操作。
答案 0 :(得分:17)
toList
可以将一个Observable的所有项目收集到List
并发出它。 toList
可以使用collect
来实现,但它比collect
容易得多。例如,它将是:
fooContainerObservable
.map(container -> container.getFooList())
.flatMap(foo -> transformFooToFooBar(foo))
.toList()
.subscribe(fooBarList -> /* Display the list */);