RxJava 替代 map() 运算符以保存发出的项目 [英] RxJava alternative for map() operator to save emitted items
本文介绍了RxJava 替代 map() 运算符以保存发出的项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用 Retrofit 与 REST API 交互,而 RxJava 会处理我收到的数据.
I use Retrofit to interact with a REST API and RxJava do manipulate the data I receive.
在下面的代码片段中,我进行了一个 API 调用并使用 map
运算符来保存我收到的数据,然后再对流进行其他操作.
In the code snippet below I make an API call and use the map
operator to save the data I receive before moving on with other operations against the stream.
retrofitApi.registerDevice(mDevice)
.map(new Func1<Device, Device>() {
@Override
public Device call(Device device) {
// The only purpose of the map() operator
// is to save the received device.
magicBox.saveDeviceId(device.getId());
return device;
}
})
.otherOperations()...
我的问题是:是否有更好的操作员来完成这项工作?我觉得我误用了 map
运算符.
My question is: is there a better operator for the job? I feel like I misuse the map
operator.
推荐答案
按照 Egor 的回答我做了一些研究,并且,基于 Dan Lew 的博文 和 这个问题,正确答案似乎是doOnNext
.
Following Egor's answer I did some research and, based on Dan Lew's blogpost and this question, the correct answer appears to be
doOnNext
.
这篇关于RxJava 替代 map() 运算符以保存发出的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文