Created
January 3, 2021 23:58
-
-
Save sturdysturge/e59b6c76d8a0b20d06dcd675fd2bb7c7 to your computer and use it in GitHub Desktop.
This file contains 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
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