NSURLCache removeCachedResponseForRequest has no effect

I created a subclass of NSURLCache that forces responses to be cached for a specific amount of time. This works well and the cached items expired as expected. However, I am running into problems when trying to forcefully delete a cached response using the NSURLCache method removeCachedResponseForRequest:

.

What I want to achieve is to force users to reload deleted data. To do this, I pass the "ignoreCache" flag when making a request. I am building my NSURLRequest as usual, but will ask NSURLCache to remove any previously cached responses for a given request. However, this does not seem to have any effect as the cached result is still present and used when the request is made.

The documentation around NSURLCache is pretty sparse. The NSURLCache headers indicate that the NSURLRequest passed in removeCachedResponseForRequest:

is used as the key to find the associated NSCachedURLResponse object, but there is little information about the logistics of this comparison. Does the NSURLCache class expect to receive the same NSURLRequest instance that generated the cached response, or does it just map the NSURL it represents?

Hopefully someone can point me in the right direction.

var error: NSError? = nil
var URLRequest: NSMutableURLRequest = self.operationManager.requestSerializer.requestWithMethod("GET", URLString: NSURL(string: request.URL, relativeToURL: self.operationManager.baseURL).absoluteString, parameters: request.parameters, error: &error)
URLRequest.cachePolicy = NSURLRequestCachePolicy.ReturnCacheDataElseLoad

// We've been asked to ignore the cache, so remove our previously cached response
if ignoreCache == true {
    NSURLCache.sharedURLCache().removeCachedResponseForRequest(URLRequest)
}

      

Here's the Swift code from my NSURLCache subclass for reference:

// MARK: - NSURLCache
override func cachedResponseForRequest(request: NSURLRequest!) -> NSCachedURLResponse! {
    var cachedResponse: NSCachedURLResponse? = super.cachedResponseForRequest(request)
    if(cachedResponse != nil && cachedResponse!.userInfo != nil) {
        var cacheDate: NSDate? = cachedResponse!.userInfo![self.cacheExpirationKey] as? NSDate
        if(cacheDate != nil) {
            var cacheDateLimit: NSDate = cacheDate!.dateByAddingTimeInterval(self.cacheExpirationInterval)
            if(cacheDate!.compare(NSDate()) == NSComparisonResult.OrderedAscending) {
                self.removeCachedResponseForRequest(request)
            } else {
                return cachedResponse
            }
        }
    }

    // Either our cached data was too old, or we don't have any that match the NSURLRequest
    return nil
}
override func storeCachedResponse(cachedResponse: NSCachedURLResponse!, forRequest request: NSURLRequest!) {
    var userInfo: NSMutableDictionary = NSMutableDictionary(dictionary: cachedResponse.userInfo)
    userInfo[cacheExpirationKey] = NSDate().dateByAddingTimeInterval(self.cacheExpirationInterval)

    var modifiedCacheResponse: NSCachedURLResponse = NSCachedURLResponse(response: cachedResponse.response, data: cachedResponse.data, userInfo: userInfo, storagePolicy: cachedResponse.storagePolicy)
    super.storeCachedResponse(modifiedCacheResponse, forRequest: request)
}

      

+3


source to share


1 answer


Assuming you are on iOS 8 removeCachedResponseForRequest:

doesn't work.



See http://blog.airsource.co.uk/2014/10/13/nsurlcache-ios8-broken-2/

+6


source







All Articles