Alamofire - NSURLCache is not working?

岁酱吖の 提交于 2019-11-28 06:06:05

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. :)

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)
Elvis Rudonja

[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 :)

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

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

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