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
| let imageCache = NSCache<NSString, UIImage>() |
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 downloadImage(url: URL, completion: @escaping (UIImage?) -> Void) |
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
| if let cachedImage = imageCache.object(forKey: url.absoluteString as NSString) { | |
| completion(cachedImage, nil) | |
| } |
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
| static func downloadImage(url: URL, completion: @escaping (_ image: UIImage?, _ error: Error? ) -> Void) { | |
| if let cachedImage = imageCache.object(forKey: url.absoluteString as NSString) { | |
| completion(cachedImage, nil) | |
| } else { | |
| MTAPIClient.downloadData(url: url) { data, response, error in | |
| if let error = error { | |
| completion(nil, error) | |
| } else if let data = data, let image = UIImage(data: data) { | |
| imageCache.setObject(image, forKey: url.absoluteString as NSString) |
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
| let url = URL(string: "http://www.google.com") |
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
| var defaultSession = URLSession(configuration: .default) |
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
| @IBOutlet weak var progressView: UIProgressView! | |
| @IBOutlet weak var downloadProgressLabel: UILabel! |
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
| final class iTunesAPIClient: NSObject { | |
| // Network stuff | |
| } |
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
| extension iTunesAPIClient: URLSessionDelegate { | |
| internal func urlSessionDidFinishEvents(forBackgroundURLSession session: URLSession) { | |
| // Calls background session completion in AppDelegate | |
| } | |
| internal func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didWriteData bytesWritten: Int64, totalBytesWritten: Int64,totalBytesExpectedToWrite: Int64) { | |
| // Gives you the URLSessionDownloadTask that is being executed | |
| // along with the total file length - totalBytesExpectedToWrite | |
| // and the current amount of data that has received up to this point - totalBytesWritten | |
| } |
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 application(_ application: UIApplication, handleEventsForBackgroundURLSession identifier: String, completionHandler: @escaping () -> Void) { | |
| // Background completion called here. | |
| } |