Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alamofire - NSURLCache is not working?

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?

like image 813
aryaxt Avatar asked Jan 05 '15 18:01

aryaxt


4 Answers

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

like image 56
Frank R Avatar answered Nov 14 '22 01:11

Frank R


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)
like image 23
aryaxt Avatar answered Nov 13 '22 23:11

aryaxt


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

like image 3
Igor Palaguta Avatar answered Nov 14 '22 01:11

Igor Palaguta


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

like image 2
Elvis Rudonja Avatar answered Nov 13 '22 23:11

Elvis Rudonja