Last active
April 9, 2024 06:46
-
-
Save SysCall97/4a7bed2dc85028e3d7a30797351f569d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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