Caches folder purged (Emptied/Cleared) automatically on iOS
Asked Answered
R

1

15

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.

Rorie answered 12/7, 2012 at 12:44 Comment(2)
As far as I know iOS removes the Caches folder of the apps when there is no more space on the device. This has nothing to do with your app. Let's say that the user has 1 Gb of space left and copies 1.2 Gb of music. iOS will try to make more space by deleting Caches folder from Apps in some order ( I guess the more it occupies , the earlier it is deleted ) . You can try to save the files in the Library folder and set their flag so they wouldn't upload to iCloud automatically.Discountenance
I (hopefully) found a working solution, check out my answer if you're ineterstedRorie
R
11

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];
Rorie answered 12/7, 2012 at 14:1 Comment(5)
It seems to be working, haven't had a problem yet, but didn't really do a full-on-testRorie
Be careful with this approach. My app was rejected in it's 20th revision, after iCloud was rolled out. You must be careful not to put too much in this folder (or at least prevent what you do put into this folder from uploading to iCloud). I was using this folder to store about 5 MB of data, and they protested because it violated iCloud storage rules. Just a heads up :) ...Gowk
NSApplicationSupport isn't saved to iCloud, only whats stored in NSDocumentsDir is stored to iCloud, and we save much more than a few couple of megabytes there (could get to 50-60MB without a problem)Rorie
Keep in mind, that application support dir is backed by iTunes.Gametocyte
Yup, definitely. Just don't forget to perform the following actions: [[NSURL URLWithString:NSApplicationSupportDir] setResourceValue:@(YES) forKey:NSURLIsExcludedFromBackupKey error:nil];Rorie

© 2022 - 2024 — McMap. All rights reserved.