Created
November 12, 2018 21:19
-
-
Save vialyx/f21c90150c632929d966d8285ed0e06f 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
... | |
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