Skip to content

Instantly share code, notes, and snippets.

@lalkrishna
Created August 30, 2021 12:15
Show Gist options
  • Save lalkrishna/60f16a49de5940a3207991f47e711449 to your computer and use it in GitHub Desktop.
Save lalkrishna/60f16a49de5940a3207991f47e711449 to your computer and use it in GitHub Desktop.
Throttled / Incremental search on Swift
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
guard !searchText.isEmpty else { searchQuery(); return }
NSObject.cancelPreviousPerformRequests(withTarget: self, selector: #selector(searchQuery), object: nil)
perform(#selector(searchQuery), with: nil, afterDelay: 0.4)
}
@objc func searchQuery() {
guard let searchText = searchBar.text else { return }
// Call API
}
// https://www.linkedin.com/in/lal-krishna-dev/
@lalkrishna
Copy link
Author

Option 2:
DispatchWorkItem

@lalkrishna
Copy link
Author

Option 3:
Combine with debounce

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment