RxJava alternative for map() operator to save emitted items

末鹿安然 提交于 2020-01-13 10:07:09

问题


I use Retrofit to interact with a REST API and RxJava do manipulate the data I receive.

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()...

My question is: is there a better operator for the job? I feel like I misuse the map operator.


回答1:


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.




回答2:


Looks like doOnEach() is what you're looking for, however haven't tried it myself.



来源:https://stackoverflow.com/questions/30149692/rxjava-alternative-for-map-operator-to-save-emitted-items

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!