Alamofire - Nsurlcache Is Not Working

Alamofire - NSURLCache is not working?

I ended up manually adding Cache-Control as private in the header of my request and it now works. Don't even need to manually check the cache, Alamofire does it for you

let cachePolicy: NSURLRequestCachePolicy = isReachable() ? .ReloadIgnoringLocalCacheData : .ReturnCacheDataElseLoad

var request = NSMutableURLRequest(URL: NSURL(string: "\(baseUrl!)\(path)")!, cachePolicy: cachePolicy, timeoutInterval: timeout)

request.addValue("private", forHTTPHeaderField: "Cache-Control")

var alamoRequest = Manager.sharedInstance.request(urlRequest)

Alamofire Cache not found

There are a few issues here.

First, currently response.request returns the last URLRequest that Alamofire saw, which is not necessarily the URLRequest that was performed and went out over the network. To see that value, you'll actually need to capture the Alamofire Request and check its task?.currentRequest property. That should give you the URLRequest that passed through Alamofire and the URLSession and was performed. We're looking into making these URLRequests available off the response as well.

Second, if you just want to check whether you received a cached response, it's better to check the URLSessionTaskMetrics value, as that should provide a definitive answer without needing to check the URLCache.

response.metrics?.transactionMetrics.last?.resourceFetchType

If this value is .localCache, then you know your response came from the cache.

AlamofireImage Disk Cache not working

To reach your goal make your NSURLCache which you use as diskCache really custom to set your own expiration date for the stored images:

class DiskCache: NSURLCache {
private let constSecondsToKeepOnDisk = 30*24*60*60 // 30 days

override func storeCachedResponse(cachedResponse: NSCachedURLResponse, forRequest request: NSURLRequest) {
var customCachedResponse = cachedResponse
// Set custom Cache-Control for Image-Response
if let response = cachedResponse.response as? NSHTTPURLResponse,
let contentType = response.allHeaderFields["Content-Type"] as? String,
var newHeaders = response.allHeaderFields as? [String: String] where contentType.containsString("image") {
newHeaders["Cache-Control"] = "public, max-age=\(constSecondsToKeepOnDisk)"
if let url = response.URL, newResponse = NSHTTPURLResponse(URL: url, statusCode: response.statusCode, HTTPVersion: "HTTP/1.1", headerFields: newHeaders) {
customCachedResponse = NSCachedURLResponse(response: newResponse, data: cachedResponse.data, userInfo: cachedResponse.userInfo, storagePolicy: cachedResponse.storagePolicy)
}
}
super.storeCachedResponse(customCachedResponse, forRequest: request)
}
}

Instead of creating a new ImageDownloader every time you could reuse the shared instance to call the downloadImage method: UIImageView.af_sharedImageDownloader.downloadImage(URLRequest: request)

How to most easily set the memory capacity of the NSURLCache to zero with AlmofireImage?

Your answer will certainly work. Here's another approach that doesn't require you to build your own ImageDownloader.

let downloader = UIImageView.af_sharedImageDownloader
let configuration = downloader.sessionManager.session.configuration
configuration.URLCache?.memoryCapacity = 0

This will end up doing the same thing without having to create any new instances of anything.



Related Topics



Leave a reply



Submit