Guest User

Untitled

a guest
Jun 21st, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.57 KB | None | 0 0
  1. class NetworkManager {
  2. // Stores the session used in this class
  3. private let session: URLSession
  4.  
  5. // Dependency Injection + default argument for normal initializ
  6. init(session: URLSession = .shared) {
  7. self.session = session
  8. }
  9.  
  10. func loadData(from url: URL,
  11. completionHandler: @escaping (NetworkResult) -> Void) {
  12. let task = session.dataTask(with: url) { data, _, error in
  13. let result = data.map(NetworkResult.success) ?? .failure(error)
  14. completionHandler(result)
  15. }
  16.  
  17. task.resume()
  18. }
  19. }
Add Comment
Please, Sign In to add comment