Skip to content

Instantly share code, notes, and snippets.

@vialyx
Created November 12, 2018 21:19
Show Gist options
  • Save vialyx/f21c90150c632929d966d8285ed0e06f to your computer and use it in GitHub Desktop.
Save vialyx/f21c90150c632929d966d8285ed0e06f to your computer and use it in GitHub Desktop.
...
func getFlights(_ type: RepositoryType, _ completion: @escaping ((Result<[FlightData]>) -> Void)) {
switch type {
case .local:
completion(.success(database.get()))
case .remote:
let resource = Resource(url: URL(string: "https://desolate-beyond-86929.herokuapp.com/arrival")!)
apiClient.load(resource) { [weak self] (result) in
switch result {
case .success(let data):
do {
let items = try JSONDecoder().decode([FlightData].self, from: data)
self?.database.save(flights: items)
completion(.success(items))
} catch {
completion(.failure(error))
}
case .failure(let error):
completion(.failure(error))
}
}
}
}
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment