Skip to content

Instantly share code, notes, and snippets.

@SysCall97
Last active April 9, 2024 06:46
Show Gist options
  • Save SysCall97/4a7bed2dc85028e3d7a30797351f569d to your computer and use it in GitHub Desktop.
Save SysCall97/4a7bed2dc85028e3d7a30797351f569d to your computer and use it in GitHub Desktop.
import Foundation
class Search {
private var suggestedCitiesWorkItem: DispatchWorkItem?
func getSuggestedCities(for prefix: String) {
// here cancelling the previous workitem before assigning the new one
suggestedCitiesWorkItem?.cancel()
let workItem: DispatchWorkItem = DispatchWorkItem {
NetworkManager.shared.getSuggestedCities(for: prefix) { response in
// Instructions for response get from the API call
}
}
suggestedCitiesWorkItem = workItem
// executing the workitem after 1 second
DispatchQueue.global().asyncAfter(deadline: .now() + .seconds(1), execute: workItem)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment