12

I, like many developers, got an email from Apple recently that stated we should move our data from the documents directory into another folder to permit more streamlined backup to iCloud.

In recent testing it appears that [your app] stores a fair amount of data in its Documents folder.

Since iCloud backups are performed daily over Wi-Fi for each user's iOS device, it's important to ensure the best possible user experience by minimizing the amount of data being stored by your app.

Marco Arment, of instapaper fame, has a good take on the issue, which is that the recommended location for storing downloadable files is in /Library/Caches. However, the problem is that both /tmp and /Caches can be 'cleaned' anytime the OS decides that the device is running low on storage. If your app is cleaned then the data downloaded by your app and stored by your user is gone. Naturally, the user will blame you and not Apple.

What to do?

Michael Morrison
  • 1,323
  • 1
  • 18
  • 30
  • Does it say you must, or just that you should consider it? If you can make an argument that transient storage is not "the best possible user experience", you may be able to legitimately say "we considered it but it is not a good user experience". – ceejayoz Oct 14 '11 at 03:36
  • 3
    I have a feeling that should will turn to must very soon. iCloud backups are going to be a massive use internet bandwidth in the coming months, perhaps a significant percentage. And remember, the user only gets 5 gig storage by default. That is why Apple is keen to have developers apps only have vital data in Documents. I just wish they would give us the option to create a non-iCloud directory that is also protected from 'cleaning'. – Michael Morrison Oct 14 '11 at 04:00

2 Answers2

15

iOS 5.0.1 introduced a flag to address this issue:

https://developer.apple.com/library/ios/#qa/qa1719/_index.html

Their recommendation is to create a folder in /Library/ like /Library/PrivateDocs , and put your files there. However you will also have to set a "do not backup" flag on them as every file in /Library except for those in /Library/Cache or tmp are backed up by default. Set the flag on PrivateDocs folder with this command:

#include <sys/xattr.h>
- (BOOL)addSkipBackupAttributeToItemAtURL:(NSURL *)URL
{
    const char* filePath = [[URL path] fileSystemRepresentation];

    const char* attrName = "com.apple.MobileBackup";
    u_int8_t attrValue = 1;

    int result = setxattr(filePath, attrName, &attrValue, sizeof(attrValue), 0, 0);
    return result == 0;
}
BadPirate
  • 25,802
  • 10
  • 92
  • 123
  • I was glad to see this in 5.0.1 and have selected this as the correct answer. Not to not agree with previous answers, which were good at the time.. its just that this is the way to do it. – Michael Morrison Nov 16 '11 at 03:21
  • 2
    The code is now deprecated. The URL is still valid though, and there you can find the code for iOS 5.1 and newer. – The dude May 01 '13 at 08:50
  • where this method is used? – Purva May 01 '13 at 11:41
3

Library/Caches is probably a good answer for many apps. Especially when the app will work fine is cached data is lost and clearing the cache does not also destroy all record of what data a user might have elected to cache and where it can be re-obtained from.

For apps which have data which does not belong in Caches consider Library/Application Support.

http://developer.apple.com/library/ios/documentation/FileManagement/Conceptual/FileSystemProgrammingGUide/FileSystemOverview/FileSystemOverview.html#//apple_ref/doc/uid/TP40010672-CH2-SW1

Application Support:

Use this directory to store all application data files except those associated with the user’s documents. For example, you might use this directory to store application-created data files, configuration files, templates, or other fixed or modifiable resources that are managed by the application. An application might use this directory to store a modifiable copy of resources contained initially in the application’s bundle. A game might use this directory to store new levels purchased by the user and downloaded from a server.

All content in this directory should be placed in a custom subdirectory whose name is that of your application’s bundle identifier or your company.

In iOS, the contents of this directory are backed up by iTunes.


Unfortunately the Application Support directory is still backed up and falls under Apple's new data storage guidelines. Depending on how sensitive reviewers choose to be about total backed up file size this may still lead to rejections.

Community
  • 1
  • 1
Jonah
  • 17,918
  • 1
  • 43
  • 70