observable java,RxJava在列表上执行操作并返回一个observable

I'm new to RxJava (specifically, RxJava2) and I'm having some trouble with what seems to be a relatively easy operation. I need to get some data from a db, iterate through the data (it is represented as a list), perform an operation on each item, wrap the data in another object and return. This is what I have so far:

mDataManager

.getStuffList(id)

.flatMapIterable(listOfStuff -> listOfStuff)

.flatMap(item ->

mDataManager

.performCount(id, item.getTitle())

.doOnNext(item::setCounter)

.takeLast(1)

.map(counter -> item))

.toList()

.toObservable()

.flatMap(listOfStuff -> Observable.just(new StuffWrapper(listOfStuff));

The problem I'm having is that my data manager calls never complete. The idea was that whenever the underlying data changes, the UI changes as well. However, without completing those calls, toList() won't emit the event.

解决方案

In RxJava 2, toList returns a Single: it's equivalent to an Observable with exactly one item. You can convert it to an Observable by adding .toObservable, but that isn't needed that often.

Regarding your other changes, what do you mean by whenever the underlying data changes? Does your data manager notify you on data changes?

Edit: if your mDataManager.getStuffList(id) call returns an Observable that emits multiple items (that is, it never completes but always emits the latest data set after a change), then you need to do something like this:

mDataManager

.getStuffList(id)

.flatMap(listOfStuff ->

Observable

.from(listOfStuff)

.flatMap(item ->

mDataManager

.performCount(id, item.getTitle())

.doOnNext(item::setCounter)

.takeLast(1)

.map(counter -> item)

.toList()

)

)

...

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值