Search code examples
androidrx-javarx-java2

RxJava 2, what happen the resultSelector argument of flatmap?


In RxJava1 flatmap had a overloaded method that allowed you to retain source values and pass it down the stream.

I've gained this knowledge from the following blog post

https://medium.com/rxjava-tidbits/rxjava-tidbits-1-use-flatmap-and-retain-original-source-value-4ec6a2de52d4

However, moving to RxJava2, I cannot seem to find it. I checked the changes from Rx1 and Rx2 and it is not listed. I would like to know if it exists still but I am perhaps not looking in the right place.

I am using a Single by the way.


Solution

  • I don't think Single ever supported this operator and the Observable/Flowable operators are still there. You can accomplish this behavior by mapping the result of the inner source:

    source.flatMap(originalValue -> 
        createInnerSource(originalValue)
        .map(innerValue -> process(originalValue, innerValue))
    )
    

    The lambda of the map will capture the originalValue for you.