When are files from NSCachesDirectory removed? - iphone

In what circumstances would files in the iOS NSCachesDirectory get removed? Obviously, delete and reinstall an application. What about application upgrade? What about low disk space conditions? Anything else?

Our experience is that this folder gets cleared on app updates. It would be nice to know when exactly this folder is a candidate for being cleared. The docs describe this folder as
location of discardable cache files (Library/Caches)
The NSDocumentDirectory will not be cleared on app updates but be careful using this folder since iOS 5 now uses this folder for backing up in iCloud and your app will likely be rejected if you use this directory to store anything other then user generated content.

Anything which is in Library folder will NOT be deleted by the iOS during App Update.
Please check following documentation from Apple which mentions:
When a user downloads an app update, iTunes installs the update in a new app directory. It then moves the user’s data files from the old installation over to the new app directory before deleting the old installation. Files in the following directories are guaranteed to be preserved during the update process:
Application_Home>/Documents
Application_Home>/Library
Although files in other user directories may also be moved over, you should not rely on them being present after an update.
Here is the link to this documentation:
http://developer.apple.com/library/ios/#documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/PerformanceTuning/PerformanceTuning.html#//apple_ref/doc/uid/TP40007072-CH8-SW10
Hope this helps.

NSCachesDirectory can be deleted in cases of low memory
On iOS 5.0 and later, the system may delete the Caches directory on
rare occasions when the system is very low on disk space. This will
never occur while an app is running. However, you should be aware that
iTunes restore is not necessarily the only condition under which the
Caches directory can be erased.
more info
If you don't want your files to be deleted you have to store them in the Documents directory, but this way:
Use the "do not back up" attribute for specifying files that should remain on device, even in low storage situations. Use this attribute with data that can be recreated but needs to persist even in low storage situations for proper functioning of your app or because customers expect it to be available during offline use. This attribute works on marked files regardless of what directory they are in, including the Documents directory. These files will not be purged and will not be included in the user's iCloud or iTunes backup. Because these files do use on-device storage space, your app is responsible for monitoring and purging these files periodically.
more info

Related

How Can I Setup My iOS App To Use Automatic iCloud Backup?

I have written an app the uses the RealmDB, which produces a file called default.realm. This is the database storage file. I would like to setup my app to make use of the iCloud automatic backup feature. How can I setup my app to do this?
You can't! Because your app is already set up like that! :)
According to the File System Basics page on Apple's website, all files in an app's Documents folder will automatically be backed up to iCloud, or the user's local iTunes account if they've chosen such. This is on by default, and must be explicitly disabled through code if disabling backup is desired.
By default, Realm places default.realm in the Documents directory for this exact reason: to ensure any user-generated data stored in it will be properly backed up in through iCloud and/or iTunes sync operations.
So you don't need to worry! Your Realm data is already being properly backed up by iCloud as we speak! :)

Apps must follow the iOS Data Storage Guidelines or they will be rejected in app that contains .sqlite3

I created an ebook app and my app contains a lot of images and pdf files
I was putting the downloaded images in /Library/Caches and putting the pdf files and .sqlite3 file in /Documents
now my app is rejected and this is the reason
2.23 Apps must follow the iOS Data Storage Guidelines or they will be rejected
Is this means that I have to move all what in /Documents to /Library/Caches?
If I let the .sqlite3 file in /Documents, will the app reject again?
Thanks in advance.
2.23 means that you should only put stuff into /Documents that cannot be re-downloaded from the internet. i.e. user-generated files. Everything that CAN be re-downloaded should be in Library/Caches.
The reason for this rule is that /Documents gets backed up and users don't like if you waste their precious iClould backup space.
You don't necessarily have to put the downloaded PDFs and databases into the Caches directory. An alternative would be to use the "do not backup" attribute, as described here: Technical Q&A QA1719 - How do I prevent files from being backed up to iCloud and iTunes?.

Can downloaded images/files be added to my app bundle?

I have an app that allows me to download characters (images), sounds, etc. When the user chooses which character to download can I store that in a directory in my App Bundle? If not, where is the best place to store my content (data)? I was thinking the documents directory, but then I saw somewhere that Apple now wants data stored in the caches directory.
Thanks for the help.
can I store that in a directory in my App Bundle?
No, you can not modify the app bundle.
If not, where is the best place to store my content (data)?
If your app must support iOS 5.0 or earlier, you will need to store your app data in the Caches directory. However, keep in mind the files could be deleted in low space situations so your app will need to degrade gracefully if your app data is deleted.
As of iOS 5.0.1, you can store your data files in the Documents Directory and flag them as "do not back up". By flagging the files, your app complies with the guideline that apps are responsible for ensuring that only user data and not application data is backed up to iCloud and iTunes and avoids a possible rejection by Apple. In this case, the files will not be deleted in low space situations.
As of iOS 5.1, you can store your data files in the Documents Directory and use either NSURLIsExcludedFromBackupKey or kCFURLIsExcludedFromBackupKey file properties to exclude files from backups. Either of these APIs is preferred over flagging them as "do not back up". In this case, your app complies with the App Store guidelines and the files will not be deleted in low space situations.
I know that this is quite a long discussion, but you should probably read it. :)
The iOS Data Storage Guidelines state:
To ensure that backups are as efficient as possible, be sure to store your app’s data according to the following guidelines:
Only documents and other data that is user-generated, or that cannot otherwise be recreated by your application, should be stored in the /Documents directory and will be automatically backed up by iCloud.
Data that can be downloaded again or regenerated should be stored in the /Library/Caches directory. Examples of files you should put in the Caches directory include database cache files and downloadable content, such as that used by magazine, newspaper, and map applications.
Data that is used only temporarily should be stored in the /tmp directory. Although these files are not backed up to iCloud, remember to delete those files when you are done with them so that they do not continue to consume space on the user’s device.
If you have an OS X or Unix background, it’s easy to understand Apple’s position that such directories have no guarantee as to how long the data in them will persist.
The fact that the tmp directory in in the app’s sandbox is not the same as the root /tmp directory, should not make a difference how you think about this directory.
If you have never observed files being removed from these directories in the past, that is not a guarantee that it will not change in the future. Especially when the change is in accordance with documentation. This is a general rule.
Changes/gaps in Apple’s documentation:
As late as June 29, 2011 Apple’s documentation regarding /Documents said:
Use this directory to store user documents and application data files.
This is pretty clear. No wonder developers are unhappy that the rules for the Documents directory have changed in iOS 5 without any suitable alternative.
Regarding /Library/Caches:
Use this directory to write any application-specific support files that you want to persist between launches of the application or during application updates. Your application is generally responsible for adding and removing these files. It should also be able to re-create these files as needed because iTunes removes them during a full restoration of the device.
App review rejections
Developers are reporting that apps that store any/some/much data in the Documents directory are being rejected by App Review.
It’s unlikely that the App Review team has detailed knowledge of which files are being stored in which directory and which of those are user generated vs. data that can be downloaded again or regenerated. Some developers have reported success in responding to the App Review team with an explanation of how their app is storing data and how that is in accordance with the rules.
What is being backed up by iTunes and iCloud
Everything in the app’s home directory is backed up, with the exception of:
The app bundle itself (.app)
/tmp directory
/Library/Caches directory
Other documentation clearly states that the Application Support directory is also backed up by iTunes (and presumably iCloud). In the discussions some developers have suggested that Application Support directory would be safer (= more permanent) alternative to Caches. To me it seems that App Review would crack down on large amounts of data stored in Application Support with the same fervor as for the Documents directory, since it’s all about iCloud storage.
Changes in behavior in iOS 5
As of iOS 5, /Library/Caches may be purged while your app is not running if the device experiences a low disk space warning.
There is no longer a directory where your app can store files that are:
Not backed up to iTunes/iCloud
Not at risk of being purged
Downloaded content should not be stored in the Documents directory according to Apple's Guidelines:
Only documents and other data that is user-generated, or that cannot otherwise be recreated by your application, should be stored in the <Application_Home>/Documents
Use the /Library/Caches directory instead
Data that is used only temporarily should be stored in the <Application_Home>/tmp directory.

When you download an iPhone application updates from Apple AppStore, is it a partial download or a complete redownload?

I want to know, when Apple AppStore pushes an application updates to the user, and when the user chooses to download that application update, is it a partial updates or is it a complete reinstall for the application?
Assuming an extreme case, the developer only updated one image in the application and he submitted the updates to the app store, does apple smartly delivers the updated portion of the app, or it blindly redeliver the whole new application to the end user as an update?
It is a complete download and reinstall of the entire application binary, with only user data being preserved. This is to mitigate any otherwise irreversible corruptions during the update, since if the update itself is corrupted then it's just a matter of rolling back to what was previously installed on a device, preventing user data loss.
From Apple's developer docs:
When a user downloads an application update, iTunes installs the update in a new application directory. It then moves the user’s data files from the old installation over to the new application directory before deleting the old installation. Files in the following directories are guaranteed to be preserved during the update process:
<Application_Home>/Documents
<Application_Home>/Library
Although files in other user directories may also be moved over, you should not rely on them being present after an update.
It doesn't say out loud that it's a complete reinstall, however the first paragraph makes it quite clear why it is.

What precisely happens when iOS installs an update to an app?

What is the official, documented behavior when you install an update to an app?
This question asks the same thing, but the answer, which says that the app bundle is replaced but all other folders are untouched, does not provide any documentation. I have reason to believe that the app bundle, in fact, is not replaced, so I'd like to know for sure.
Please link to documentation!
From the iOS Application Programming Guide (see the "Files Saved During Application Updates" section within "The File System"):
When a user downloads an application
update, iTunes installs the update in
a new application directory. It then
moves the user’s data files from the
old installation over to the new
application directory before deleting
the old installation. Files in the
following directories are guaranteed
to be preserved during the update
process:
<Application_Home>/Documents
<Application_Home>/Library
Although
files in other user directories may
also be moved over, you should not
rely on them being present after an
update.
What you're seeing in the Why isn't my iPhone app bundle replacing the old one during an update? question is an issue where Xcode doesn't correctly detect/push updated resources to the device, which is a very different scenario.