On iOS where is the NSURLCache cache stored if diskPath:nil?

nsurlcache not caching
ios caching
ios webview cache
ios cache json response
ios clear cache programmatically
nsurlconnection cache
nsurlcache example objective-c
moya caching

I've come across code that looks like this:

NSURLCache *URLCache = 
    [[NSURLCache alloc] initWithMemoryCapacity:1024 * 1024 
                                  diskCapacity:1024 * 1024 * 5 
                                      diskPath:nil];

The problem is that I haven't been able to find what's the expected behavior when diskPath is passed nil. NSURLCache docs don't explicitly describe this situation nor I have been able to figure it out with my own testing.

Where is the cache stored? or is that code above a bug?

Thanks.

I had some free time today to do some testing and found the answer. Nothing exciting but if you are curious:

By default iOS will use a database named Cache.db and, as @qegal mentioned, will be stored under the default location.

On iOS, where is the NSURLCache cache stored if diskPath: nil , I had some free time today to do some testing and found the answer. Nothing exciting but if you are curious: By default iOS will use a database named Cache.​db  It provides a composite in-memory and on-disk cache, and lets you manipulate the sizes of both the in-memory and on-disk portions. You can also control the path where cache data is persistently stored. Note. In iOS, the on-disk cache may be purged when the system runs low on disk space, but only when your app is not running.

From your linked doc:

In iOS, path is the name of a subdirectory of the application’s default cache directory in which to store the on-disk cache (the subdirectory is created if it does not exist).

My best guess would be that if path is nil (meaning you did not specify a subdirectory), the cache would just be stored in the default cache directory (and not in a subdirectory). I can't test it out right now, because I'm having some computer issues. I'm also guessing that it would be stored in the default cache directory because I'm sure you would get some sort of warning or error either at runtime or when you're writing the code. I know if I wrote something like this:

[array writeToFile: nil atomically:NO];

I would get an error as I did not specify the path to which the file should be written to.

Hope this helps!

NSURLCache, Today I had free time to do some testing and find the answer. Nothing interesting, but if you're interested: By default, iOS will use a database called Cache.db  Mattress. iOS Offline Caching for Web Content. A Swift framework for storing entire web pages into a disk cache distinct from, but interoperable with, the standard NSURLCache layer.

Under iOS 8.2 the Cache.db file is stored at:

(App Folder)/Library/Caches/com.mycompany.appname/Cache.db

Where com.mycompany.appname is your Bundle ID

URLCache, The problem is that I haven't been able to find what's the expected behavior when diskPath is passed nil. NSURLCache docs don't explicitly describe this  The current implementation of NSURLCache doesn't save to disk, even if requests are marked this way. With AFURLCache, images and requests that are marked as cacheable are now really cached to disk, loaded almost instantaneous. Even etags are parsed. Check it out. I'd love to see this in main.

If diskPath == xxxx, the Cache.db file is store at:

(App Folder)/Library/Caches/com.mycompany.appname/xxxx/Cache.db

A Swift framework for storing entire web pages into a disk cache , As of iOS 5, a shared NSURLCache is set for the application by default. 1024, diskCapacity: 20 * 1024 * 1024, diskPath: nil) NSURLCache. NSURLCache provides a composite in-memory and on-disk caching mechanism for URL requests to your application. As part of Foundation’s URL Loading System, any request loaded through NSURLConnection will be handled by NSURLCache. Network caching reduces the number of requests that need to be made to the server,

Hot questions for On iOS where is the NSURLCache cache stored if , The URLCache class implements the caching of responses to URL load requests​, by mapping NSURLRequest You can also control the path where cache data is persistently stored. In iOS 8 and later, and macOS 10.10 and later, URLCache is thread safe. init(memoryCapacity: Int, diskCapacity: Int, diskPath: String?) It provides a composite in-memory and on-disk cache, and lets you manipulate the sizes of both the in-memory and on-disk portions. You can also control the path where cache data is persistently stored. Note. In iOS, the on-disk cache may be purged when the system runs low on disk space, but only when your app is not running.

How to delete download image data in cache.db in device(iphone , I've come across code that looks like this: NSURLCache *URLCache = [[​NSURLCache alloc] initWithMemoryCapacity:1024 * 1024  Clone via HTTPS Clone with Git or checkout with SVN using the repository’s web address.

iOS 7.0+ (iOS 8 required for integration as an embedded framework) You should create an instance of URLCache and set it as the shared diskPath: nil, mattressDiskCapacity: 1 * GB, mattressDiskPath: nil, mattressSearchPathDirectory: . "https://www.google.com") if let cache = NSURLCache. This will shift the URLCache from NSURLCache to your CustomURLCache. The parameters: disPath is the path where you want to store. if set to nil, it equals the default cache directory. cacheTime is the expiring time of the caches.if set to 0, the cache will never be expired. subDirectory is a subdirectory in the diskPath.

Comments
  • Can you share how would you determine whether the cache was stored in the default directory? I can try it on my end.
  • @jlmendezbonini Do you mean how you would verify that the cache was stored at all?
  • Yes. I might be missing something obvious on how to do that.
  • @jlmendezbonini - Well I'm not very familiar with caches, but I'm pretty sure they store web documents like images and keychains, so couldn't you just make the diskPath:nil and then go to the web page where you want to get the cache from. For example, I think SO stores a keychain cache on my computer, so I don't have to login every time I reload the page. Try setting the diskPath:nil and then logging on to SO. Then close the page and quit Safari. Then open it back up again and go to SO, and if you are logged on, then the cache was successfully stored.