Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

flatMap vs map + switchToLatest #132

Open
mycroftcanner opened this issue Nov 24, 2019 · 1 comment
Open

flatMap vs map + switchToLatest #132

mycroftcanner opened this issue Nov 24, 2019 · 1 comment
Labels
question Further information is requested

Comments

@mycroftcanner
Copy link

mycroftcanner commented Nov 24, 2019

If you want to use data provided by a publisher as a parameter or input to creating this publisher, there are two common means of enabling this:

Using the flatMap operator, using the data passed in to create or return a Publisher instance. This is a variation of the pattern illustrated in Using flatMap with catch to handle errors.

Alternately, map or tryMap can be used to create an instance of a publisher, followed immediately by chaining switchToLatest to resolve that publisher into a value (or values) to be passed within the pipeline.

The difference between the two alternative ways to create a publisher should be clearer.

When is it okay to use flatMap and when is it okay to use switchToLatest?

This is quite confusing.

@heckj
Copy link
Owner

heckj commented Nov 24, 2019

That's a really good question, to which I don't have a definitive answer - and didn't feel like I could assert one without better internal knowledge.

To my experiments, the two systems appear to operate identically - although I didn't benchmark them to any insane degree to see if there was a slight performance difference between using the two structures. In practice, I think they're symmetrical paths and its a choice of pattern that you prefer, but I'm not sufficiently confident in my understanding to assert that in the book. I also don't have any inside information into the creation/underlying code of Combine to know if there's a subtle difference there that should be called out, so I'm afraid I've just them both as open parallel paths.

@heckj heckj added the question Further information is requested label Nov 24, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants