Daily iOS
SwiftUI

switchToLatest search pipeline

You're building a Combine pipeline for a SwiftUI search screen that calls the API as the user types. What's the difference between putting switchToLatest() after map { query in api.search(query) } versus using flatMap, and why is switchToLatest the right choice for search?

How would an interviewer hear this answer? You'll see whether they'd move on, dig in, or say no, and which sentences would trip you up. Your score doesn't change.
See the short answer

switchToLatest cancels the previous inner Publisher when a new one arrives and subscribes only to the latest, so responses never arrive out of order. flatMap keeps every request running in parallel, so a late response can overwrite the latest results.

Share a question