Skip to content

Instantly share code, notes, and snippets.

@wilmtang
Last active February 13, 2020 22:14
Show Gist options
  • Select an option

  • Save wilmtang/4ab7f968b6fdddae34c088fa84e29271 to your computer and use it in GitHub Desktop.

Select an option

Save wilmtang/4ab7f968b6fdddae34c088fa84e29271 to your computer and use it in GitHub Desktop.
struct Resource<T> {
let url: URL
// Other properties and methods
}
class NetworkManager {
func load<T>(resource: Resource<T>, withCompletion completion: @escaping (T?) -> Void) {
let session = URLSession(configuration: .ephemeral, delegate: nil, delegateQueue: .main)
let task = session.dataTask(with: resource.url, completionHandler: { (data: Data?, response: URLResponse?, error: Error?) -> Void in
guard let data = data else {
completion(nil)
return
}
// Use the Resource struct to parse data
})
task.resume()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment