5

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.

Tudor Luca
  • 6,259
  • 2
  • 29
  • 44

2 Answers2

7

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.

Community
  • 1
  • 1
Tudor Luca
  • 6,259
  • 2
  • 29
  • 44
3

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

Egor
  • 39,695
  • 10
  • 113
  • 130