0

I'm tying to save some response to URLCache:

var cache: URLCache = URLCache(memoryCapacity: 4 * 1024 * 1024, diskCapacity: 10 * 1024 * 1024, diskPath: nil)    
request.response { (dataResponse) in
            guard let response = dataResponse.response,
                let data = dataResponse.data,
                let request = request.request else { return }
            let cachedResponse = CachedURLResponse(response: response, data: data, userInfo: nil, storagePolicy: .allowed)
            print(cachedResponse)
            self.cache.storeCachedResponse(cachedResponse, for: request)
            print(self.cache.cachedResponse(for: request))
            
        }

But output is nil:

NSCachedURLResponse: 0x6000000053e0

nil

However size of cache used memory was increasing.

I've tried to create my own cache or using URLCache.shared instance

But response was always nil

Also I've tried to use another method of URLCache, that saves CachedResponse for task, and it save it only in case when task was in running state, but not in suspended state:

cache.storeCachedResponse(cachedResponse, for: task)

I'm really can't understand, whats happens inside this class.

P.S. I've read all article about URLCache, like these: http://nshipster.com/nsurlcache/ https://blackpixel.com/writing/2012/05/caching-and-nsurlconnection.html

Community
  • 1
  • 1
givip
  • 21
  • 1
  • 3
  • I assume, that methods of URLCache won't working with Alamofire request. But perfectly works with requests created directly by URLSession. – givip Mar 01 '17 at 16:40
  • Did you manage to figure this out? – Gasper Jun 27 '17 at 14:09
  • 1
    Yes, don't use alamofire response. Use native NSURLSession Response – givip Jun 29 '17 at 05:03
  • How do you get the 'native NSURLSession Response'? I tried creating new HTTPURLResponse(url: response.url!, statusCode: response.statusCode, httpVersion: "HTTP/1.1", headerFields: hackedHeaders), still not cached. – androidguy Nov 21 '20 at 03:04
  • I don't remember exactly, but I assume it should be stored inside Alamofire wrapper response. – givip Oct 07 '22 at 06:25

0 Answers0