12

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.

jlmendezbonini
  • 2,239
  • 21
  • 25

5 Answers5

15

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.

jlmendezbonini
  • 2,239
  • 21
  • 25
3

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!

pasawaya
  • 11,515
  • 7
  • 53
  • 92
  • Can you share how would you determine whether the cache was stored in the default directory? I can try it on my end. – jlmendezbonini Jul 02 '12 at 21:53
  • @jlmendezbonini Do you mean how you would verify that the cache was stored at all? – pasawaya Jul 03 '12 at 01:33
  • Yes. I might be missing something obvious on how to do that. – jlmendezbonini Jul 03 '12 at 03:01
  • @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. – pasawaya Jul 03 '12 at 14:08
2

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

Ben Clayton
  • 80,996
  • 26
  • 120
  • 129
1

You can find simulator's cached files below path:

~/Library/Developer/CoreSimulator/Devices/UDID/data/Containers/Data/Application/UDID/Library/Caches/BUNDLE_IDENTIFIER

Folder must be look like this:

  • Cache.db
  • Cache.db-wal (view cached and stored values here)
  • Cache.db-shm

In my experience i had to delete all previous devices from devices folders, if you cannot find the bundleIdentifier try to delete all of device folders and run the application again, that way new folder will be created on that empty folder and you can view cached files from there

Andrew
  • 2,046
  • 1
  • 24
  • 37
trkclk
  • 21
  • 3
0

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

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