Created
April 29, 2019 19:22
-
-
Save Coder-ACJHP/a26382b66b5689a2d059e9f2a9340f9a to your computer and use it in GitHub Desktop.
This class is download manager that download files from given url with caching data in disk of device for iOS
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
// | |
// DownloadManager.swift | |
// UrlCacheExample | |
// | |
// Created by Coder ACJHP on 12.04.2019. | |
// Copyright © 2019 Onur Işık. All rights reserved. | |
// | |
import UIKit | |
internal struct Gif { | |
var name: String! | |
var data: Data! | |
var coverImage: UIImage! | |
} | |
class DownloadManager: NSObject { | |
private let allowedDiskSize = 100 * 1024 * 1024 | |
private lazy var cache: URLCache = { | |
return URLCache(memoryCapacity: 0, diskCapacity: allowedDiskSize, diskPath: "gifCache") | |
}() | |
private var urlStringList: [String]! | |
typealias GetResultListCompletionHandler = (Array<Gif>) -> () | |
typealias DownloadCompletionHandler = (Result<Data,Error>) -> () | |
init(urlStringList: [String]) { | |
self.urlStringList = urlStringList | |
} | |
public func getResultList(completionHandler: @escaping GetResultListCompletionHandler) { | |
var dataList: Array<Gif> = [] | |
let dispatchGroup = DispatchGroup() | |
for index in 0 ..< self.urlStringList.count { | |
dispatchGroup.enter() | |
self.downloadContent(fromUrlString: self.urlStringList[index], completionHandler: { (result) in | |
switch result { | |
case .success(let gifData): | |
let fileName = self.getFileName(urlString: self.urlStringList[index]) | |
dataList.append(Gif(name: fileName, | |
data: gifData, | |
coverImage: UIImage(named: "demoImage\(index)"))) | |
dispatchGroup.leave() | |
case .failure(let error): | |
debugPrint(error.localizedDescription) | |
dispatchGroup.leave() | |
} | |
}) | |
} | |
dispatchGroup.wait() | |
completionHandler(dataList) | |
} | |
private func downloadContent(fromUrlString: String, completionHandler: @escaping DownloadCompletionHandler) { | |
guard let downloadUrl = URL(string: fromUrlString) else { return } | |
let urlRequest = URLRequest(url: downloadUrl) | |
if let cachedData = self.cache.cachedResponse(for: urlRequest) { | |
print("Cached data in bytes:", cachedData.data) | |
completionHandler(.success(cachedData.data)) | |
} else { | |
createAndRetrieveURLSession().dataTask(with: urlRequest) { (data, response, error) in | |
if let error = error { | |
completionHandler(.failure(error)) | |
} else { | |
let cachedData = CachedURLResponse(response: response!, data: data!) | |
self.cache.storeCachedResponse(cachedData, for: urlRequest) | |
completionHandler(.success(data!)) | |
} | |
}.resume() | |
} | |
} | |
private func createAndRetrieveURLSession() -> URLSession { | |
let sessionConfiguration = URLSessionConfiguration.default | |
sessionConfiguration.requestCachePolicy = .returnCacheDataElseLoad | |
sessionConfiguration.urlCache = cache | |
return URLSession(configuration: sessionConfiguration) | |
} | |
private func getFileName(urlString: String) -> String { | |
guard let fileName = NSURL(fileURLWithPath: urlString).deletingPathExtension?.lastPathComponent else {return "" } | |
return fileName | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment