Alamofire - NSURLCache is not working?

对着背影说爱祢 提交于 2019-12-17 17:58:40

问题


I set my cache as below

var cacheSizeMemory = 20 * 1024 * 1024
var cacheSizeDisk = 100 * 1024 * 1024
var sharedCache = NSURLCache(memoryCapacity: cacheSizeMemory, diskCapacity: cacheSizeDisk, diskPath: "SOME_PATH")
NSURLCache.setSharedURLCache(sharedCache)

Create request with cache policy

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

Make a request and get a response with following Cache-Control private, max-age=60

Then try to check the cache

var cachedResponse = NSURLCache.sharedURLCache().cachedResponseForRequest(urlRequest)

value is nil

Any thoughts?


回答1:


I was able to manually cache pages by writing them to the sharedURLCache like this:

    Alamofire.request(req)
        .response {(request, res, data, error) in
            let cachedURLResponse = NSCachedURLResponse(response: res!, data: (data as NSData), userInfo: nil, storagePolicy: .Allowed)
            NSURLCache.sharedURLCache().storeCachedResponse(cachedURLResponse, forRequest: request)
        }

NSURLCache seems to respect the headers sent by the server, even if you configure the opposite everywhere else in your code.

The Wikipedia API, for example, sends

Cache-control: private, must-revalidate, max-age=0

Which translates to: Must revalidate after 0 seconds.
So NSURLCache says: “OK, I won’t cache anything.”

But by manually saving the response to the cache, it works. At least on iOS 8.2.

Almost lost my mind on this one. :)




回答2:


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)



回答3:


[Swift solution for resolving expiration of NSURLcache]

I think that main problem here is this: ReturnCacheDataElseLoad.

@arayax gave you the answer that will fix that probably, but my solution would be something like this:

Since I'm using Alamofire for Network requests I've set my configuration:

  configuration.requestCachePolicy = .ReturnCacheDataElseLoad

And when I make request I do check internet connectivity, if it is true, then clear NSURLCache, so it will force Alamofire to make request on server and not from cache:

    if Reachability.isConnectedToNetwork() == true {
        ConfigService.cache.removeAllCachedResponses()
    }

    ConfigService.manager?.request(.GET, ...

I hope this will help, maybe for other type of problems with NSURLCache :)




回答4:


For me it was Pragma →no-cache after removing this everything worked.




回答5:


I found that URLCache does not save responses bigger than 5% (1/20) of capacity.

Default cache has memoryCapacity = 512000, it does not save to memory responses greater than 25600.

As a solution extend capacity




回答6:


This is how I got the cache to work with Alamofire 4 and swift 3 (Semi full function for reference):

 func getTheList(courseId : String )->  Void{
        appConstants.sharedInstance.startLoading()
        let TheURL = DEFAULT_APP_URL + "api/getMyList?Id="+ID
        let urlString = NSURL(string: TheURL)
        var mutableURLRequest = URLRequest(url: urlString! as URL)
        mutableURLRequest.httpMethod = "GET"
        mutableURLRequest.setValue("application/json", forHTTPHeaderField: "Content-Type")
        mutableURLRequest.cachePolicy = NSURLRequest.CachePolicy.returnCacheDataElseLoad


        Alamofire.request(mutableURLRequest)
            .responseJSON
           {.......


来源:https://stackoverflow.com/questions/27785693/alamofire-nsurlcache-is-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!