Daily iOS
Concurrency

Latest response wins

On a tax summary screen, changing the filter quickly makes several API requests finish almost at once. To make sure only the result for the last filter chosen ends up on screen, how would you manage the requests and the state changes?

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

Every time the filter changes, cancel the previous request's Task and tag the new request with a generation number, then right before putting the response into state, check that its number matches the current generation and drop stale responses. Change state in one place only, a @MainActor ViewModel.

Share a question