Skip to content

Instantly share code, notes, and snippets.

@13hoop
Created September 24, 2021 07:00
Show Gist options
  • Select an option

  • Save 13hoop/e81fdb3955341b3e076c840b12dd1f13 to your computer and use it in GitHub Desktop.

Select an option

Save 13hoop/e81fdb3955341b3e076c840b12dd1f13 to your computer and use it in GitHub Desktop.
this is a mock API server demo
class Server {
private init() {}
static let shared = Server.init()
private static let serverQueue = DispatchQueue.global(qos: .background)
func getCountries(completion: @escaping (Array<String>) -> Void) {
let countries = ["France", "Germany", "Spain", "Portugal"]
let delay = Int.random(in: 1..<4)
Self.serverQueue.asyncAfter(deadline: .now() + .seconds(delay)) {
completion(countries)
}
}
func getCapital(of country:String, completion: @escaping (String) -> Void) {
let capitals = ["France":"Paris", "Germany":"Berlin", "Spain":"Madrid", "Portugal":"Lisbon"]
let delay = Int.random(in: 1..<10)
Self.serverQueue.asyncAfter(deadline: .now() + .seconds(delay)) {
completion(capitals[country, default:"N/A"])
}
}
}
@13hoop

13hoop commented Sep 24, 2021

Copy link
Copy Markdown
Author

share from here

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