Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Caches folder purged (Emptied/Cleared) automatically on iOS

I have an app that lets you download "modules" that can expand your app usage.
When the user downloads a module, I fetch a ZIP file from a server and extract it to his Caches folder. (Each of these zips could be sized anywhere from 60k to 2MB).

Unfortunately, there are over 300 modules available, and many of the users download at least 50-60 of these to their device.

Lately, I got many complaints that modules just disappear off the user device, so I did some investigation and came across the following wording in Apple's documentation.

iOS will delete your files from the Caches directory when necessary, so your app will need to degrade gracefully if it's data files are deleted.

And also the following article explaining further about this situation: http://iphoneincubator.com/blog/data-management/local-file-storage-in-ios-5

My problem is, I have no actual way of degrading gracefully, since I can't automatically let the user download so many modules. It could take hours depending on the internet connection and size of the modules.

So I have a few questions:

  1. Did any of you ever had to deal with a similar situation, and if yes, how?
  2. Does anyone know when exactly iOS purges the Cache? What is considered "low space" warning? This way I could at least give the user a warning that he doesnt have enough space to install a new module.
  3. Is there a way to receive some sort of warning before the Cache folder is cleared?

This is a really frustrating move from Apple and I don't really see a way out. Would really love to hear some ideas from you.

like image 656
Shai Mishali Avatar asked Jul 12 '12 12:07

Shai Mishali


1 Answers

Edit: After some testing, this seems to work just fine and doesn't get purged.

This isn't 100% confirmed yet, but it worked fine on our basic tests (I'll post more thorough results as they come). It seems saving the data to the app's "Application Support" folder resolves these issues, as this folder isn't purged.

The docs state:

Use the Application Support directory constant NSApplicationSupportDirectory, appending your <bundle_ID> for: Resource and data files that your app creates and manages for the user. You might use this directory to store app state information, computed or downloaded data, or even user created data that you manage on behalf of the user / Autosave files.`

You could get to that folder as follows (Notice appending of the bundle ID as requested by the official apple docs):

[[NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:[[NSBundle mainBundle] bundleIdentifier]]

Hope this helps anyone , and as I said, I will post more thorough test results during the weekend.

Edit 2: Its very important to add this line to prevent syncing of temporary content from Application Support. I use it in the end of my applicationDidFinishLaunching:

[[NSURL URLWithString:NSApplicationSupportDir] setResourceValue:@(YES) forKey:NSURLIsExcludedFromBackupKey error:nil];
like image 84
Shai Mishali Avatar answered Oct 31 '22 19:10

Shai Mishali