简体   繁体   中英

Caching downloaded images: FileManager / CachePolicy / URLCache / NSCache?

I'm need to implement the common scenario of having a table view with N cells where each of those cells needs to download an image to be displayed within it.

The service protocol to call to download the images could be either HTTP or HTTPS.

I am using an URLSessionDownloadTask this way:

func downloadImage(urlStr: String, completion: @escaping (UIImage?, Error?) -> Void) {
    let url = URL(string: urlStr)
    let request = URLRequest(url: url!)
    let task = session.downloadTask(with: request, completionHandler: {
        (fileUrl, response, error) in
        // Call 'completion' depending on result
    })

    task.resume()
}

Where session is an URLSession with default configuration and associated operation queue:

self.session = URLSession(configuration: configuration, delegate: nil, delegateQueue: self.operationQueue)

So, what I want is to avoid to download an image that was already downloaded. And I would like them to have some expiration time.

I've read some articles and posts and I'm not completely clear about the differences between the options I found:

A. Using FileManager to actually store the image as a file, and removing it after checking the expiration time.

B. Setting the cachePolicy property of URLRequest .

C. Using URLCache

D. Using NSCache

About A:

  1. What is actually the difference between storing the image as file and using a cache? Could have the file storage offer any benefit that a cache does not? Those images are not user-related, I can download them from a server when needed.

About B:

  1. I read Apple's documentation about that, but I don't fully understand if for my scenario I should use NSURLRequestUseProtocolCachePolicy .
  2. How does this option actually work? It is enough to set the policy and then you don't have to care about anything else? How does the URLRequest now that the image is asked for download has been already downloaded and cached?

About C:

  1. How does it should be correctly implemented? Could anybody provide me an example/tutorial in case this is the best approach? What about expiration date?

About D:

  1. I found an example I understood, but would it be a good approach having the previous options? What about expiration date also here?

In summary: which of the options would be the most efficient and appropriate for my scenario, and why?

From what I inferred about your question "what I want is to avoid to download an image that was already downloaded. And I would like them to have some expiration time."

To avoid images from downloading again, you can implement the following use case where, you store the images in the NSCache using the urls of the image itself.

This would be something like as discussed in the link .

For the expiration time case, if you want to remove all images at a particular expiration time, then just make a check for that scenario and empty the cache.

For the case where you want to remove the individual images , based on their expiration time, you can check the response from the server for the expiration key, and again remove cache in case the limit has been breached.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM