Skip to content

Instantly share code, notes, and snippets.

@sturdysturge
Created January 3, 2021 23:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sturdysturge/e59b6c76d8a0b20d06dcd675fd2bb7c7 to your computer and use it in GitHub Desktop.
Save sturdysturge/e59b6c76d8a0b20d06dcd675fd2bb7c7 to your computer and use it in GitHub Desktop.
extension DataModel: URLSessionDownloadDelegate {
func urlSession(_ session: URLSession,
downloadTask: URLSessionDownloadTask,
didFinishDownloadingTo location: URL) {
if let data = try? Data(contentsOf: location) {
let image = UIImage(data: data)
DispatchQueue.main.async {
self.image = image
}
}
}
func urlSession(_ session: URLSession,
downloadTask: URLSessionDownloadTask,
didWriteData bytesWritten: Int64,
totalBytesWritten: Int64,
totalBytesExpectedToWrite: Int64) {
if let download = activeDownload {
download.progress = Float(totalBytesWritten)/Float(totalBytesExpectedToWrite)
}
}
var downloadsSession : URLSession {
get {
let config = URLSessionConfiguration.background(withIdentifier: "background")
let queue = OperationQueue()
return URLSession(configuration: config, delegate: self, delegateQueue: queue)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment