0

I am trying to configure NSURLCache with the maximum disc capacity possible. I'm on iOS 7.

according to the docs, NSURLCache's discCapacity attribute is a NSUInteger, aka unsigned int, which should accept a maximum value of 4,294,967,295 (0xFFFFFFFF). However, using any value above 0x7FFFFFFF (2147483647) results in the value actually reporting itself as 0 and the disc cache isn't working at all.

Furthermore, using 0x7FFFFFFF (2GB), actually results in malloc errors as soon as the disc cache fills up anywhere close to the 2GB.

The only way i found this working is with a maximum value of 1GB (1073741824). When using this value, all works as expected.

I would like to use NSURLCache with as much disc capacity as possible for my application. How can i use 8 or even 16GB?

Update [adding code example as requested in comments]:

NSURLCache* sharedCache = [[NSURLCache alloc] initWithMemoryCapacity:sizeInBytes
                                                        diskCapacity:sizeInBytes
                                                            diskPath:@"somepath"];

[NSURLCache setSharedURLCache:sharedCache];

or you can also do:

[[NSURLCache sharedURLCache] setDiskCapacity:sizeInBytes];
[[NSURLCache sharedURLCache] setMemoryCapacity:sizeInBytes];
Reneli
  • 1,756
  • 2
  • 16
  • 26

2 Answers2

1

I'd comment, but since I'm not allowed I'll add it as an answer.

This does seem to be the case that NSURLCache only allows 2GB - anything above this value will be silently ignored/fail and the cache will not function (will not return any cached responses, nor will anything be written to the disk).

Even trying to use an existing cache by specifying the path with any value above 2GB for capacity will result in the same 'non-working' cache.

I'd suggest filing a bug with Apple to either properly implement the functionality (yes some of us have a valid reason to create large caches), or to properly document its shortcomings.

This behaviour is still present in High Sierra (macOS 1.13), and I assume the same applies to current version of iOS (11).

coderSeb
  • 111
  • 1
  • 6
  • We solved this problem by creating a subclass of NSURLCache and overriding the calls that retrieve/cache the content (`storeCachedReponse...`,`cachedResponseForRequest...`,etc.) to instead store them in a location of our choosing, as files. We even do on-the-fly de/compression of data. This allows us to set our own limits on the size, and location so the system doesn't wipe cached content behind our back (useful on iOS, since there's less control for cache location on NSURLCache). A subclass is needed if used with NSURLSession, as that uses private properties of NSURLCache. – coderSeb Nov 23 '19 at 18:58
0

According to the iTunes Connect Developer Guide there's a hard 2 GB limit on the size of an app. This includes the app bundle, the documents folder, and the tmp folder, the library folder (where caches are stored on disk), etc.

neilco
  • 7,964
  • 2
  • 36
  • 41
  • 1
    I don't interpret this statement as applicable to the issue i encounter. The guidelines refer to the app distribution bundle, i.e. everything you ship in an .ipa. Caches, as that of NSURLCache, that vary in size during runtime, are not part of the app diet bundle. Just my 2c. – Reneli Oct 29 '13 at 18:16