In my iPhone app I\'m caching the raw data of some compressed files to save loading time. But those files may change in an update.
Will iOS clear /Library/Caches for me
The whole point of /Caches is that the system can clear data stored there at any time - if it needs to. You should not worry about clearing out that directory.
To be safe, you should clear it yourself. There does not seem to be any documentation stating that it will be cleared for you, so even if it does now you cannot count on it continuing to do so.
It's up to you! Quote from Apple's documentation:
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. In iOS 2.2 and later, the contents of this directory are not backed up by iTunes.
The caches directory will be purged if iOS is running out of space. If the user chooses "update all" from the app store options, it is likely that they will run out of space.
You should only store files in the Caches directory if they can be recreated by your app. From what I understand, the media player in iOS7 will only play videos stored in caches directory, and here's how I clear them manually (just in case):
-(void)deleteAllVideos
{
NSString *cachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSFileManager* fileManager = [NSFileManager defaultManager];
NSArray *directoryContent = [fileManager contentsOfDirectoryAtPath:cachePath error:NULL];
NSError* error;
NSString* fullFilePath = nil;
for(NSString* fileName in directoryContent)
{
if([fileName hasSuffix:@".mp4"])
{
fullFilePath = [cachePath stringByAppendingPathComponent:fileName];
[fileManager removeItemAtPath:fullFilePath error:&error];
if(error)
{
DLog(@"Error: %@",error);
}
}
}
}
Short:
Will iOS clear /Library/Caches for me when the app is updated
No
Is it possible that iOS does clear everything or parts of Application_Home/Library/Caches
during an update? Yes
or I need to clear it myself?
You need to ensure what you want to be cleared is cleared.
Be prepared for both situations.
How do you know whether your app got updated? See iOS Application Update Test
Long:
Files Saved During Application Updates 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.
From Apples Documentation: The Application Runtime Environment - Backup and Restore
Application_Home/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. In iOS 2.2 and later, the contents of this directory are not backed up by iTunes.
From Apples Documentation: The Application Runtime Environment - The File System (daveoncode)
Here an example of when it does clear the cache during an "update":
You install app X V1.0. Lunch it once and the start doing something else. iOS malfunctions and needs to be restored. Developer releases V1.1. You update the app in iTunes on your Mac. You restore your device via iTunes. iTunes installs V1.1. User lunches your app. Your app does not get notified that any of that stuff happened but all files in Application_Home/Library/Cache
are gone (iTunes removes them during a full restoration of the device
).
Really important information in there: It should also be able to re-create these files as needed
. Like Kendall also pointed out it is a cache, so files could be deleted from it by the operating system at any point. So every time you load any file from Application_Home/Library/Caches
you have to account for the case that the file isn't there anymore (Kendall touched on this).
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *cachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0];
if(![fileManager fileExistsAtPath:[cachePath stringByAppendingPathComponent:@"myfile.zip"]]) {
//create it, copy it from app bundle, download it etc.
}
//start using it