gpt4 book ai didi

android - 使用 RxJava 获取对象,转换包含列表,并使用列表

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:25:02 26 4
gpt4 key购买 nike

我试图解决的高级问题是将获取的 FooContainer(可观察)中包含的 Foo 对象列表转换为 FooBar 列表 使用 RxJava 的对象。

我的(困惑的)尝试:

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 应用中执行此操作。

最佳答案

toList 可以将一个 Observable 的所有项目收集到一个 List 中并发出它。 toList可以用collect来实现,但是比collect简单多了。对于您的示例,它将是:

fooContainerObservable
.map(container -> container.getFooList())
.flatMap(foo -> transformFooToFooBar(foo))
.toList()
.subscribe(fooBarList -> /* Display the list */);

关于android - 使用 RxJava 获取对象,转换包含列表,并使用列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27362799/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com