【问题标题】:Using RxJava to fetch object, transform a containing list, and use the list使用 RxJava 获取对象,转换包含列表,并使用列表
【发布时间】:2014-12-08 16:50:48
【问题描述】:

我要解决的高级问题是使用 RxJava 将获取的 FooContainer(可观察)中包含的 Foo 对象列表转换为 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 应用中执行此操作。

【问题讨论】:

  • 你能解释一下为什么你打算使用collect吗?如果您想将所有项目收集到一个列表中,只需致电toList
  • @zsxwing 因为我从事件流中“收集”多个项目似乎是有道理的:)。我不知道toList 存在。如果您回答建议,我会将其标记为解决方案!

标签: android rx-java rx-android


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2018-01-05
    • 1970-01-01
    • 2016-02-21
    • 1970-01-01
    • 2023-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多