Daily iOS
Concurrency

Search debounce

Calling the API on every keystroke in a search field floods it with requests. Can you implement a debounce with GCD yourself, so the call only fires when there's been no further input for 300ms after the last keystroke?

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

Keep a DispatchWorkItem in a property, cancel() the previous item on every keystroke, then schedule a new one with asyncAfter. cancel only stops items that haven't started yet. In Swift Concurrency, keep and cancel a Task that runs after Task.sleep(300ms).

Share a question