Large files uploaded to Documents and supported by iCloud

I have an iOS application in the app store that can download relatively large files that need to be left on the device for offline use. These files are currently stored in the app Documents folder, but I am only now reading that the Documents folder is created and should really be used only for user-generated content. This Apple Q&A indicates that the NSURLIsExcludedFromBackupKey parameter must be set to prevent backups. It states that the /Library/Caches is the right place to place these files, although further reading suggests that the folder may be empty when the device is at a low storage level, which is unacceptable for this application. I think /Library/Application Support/ is the best place for them - does it sound right?

Unfortunately, this error went through the process of viewing applications. What are some recommendations for fixing this issue now that people are using the application and already have some files in the Documents folder and in their backups? It seems I need to move all existing files and install them NSURLIsExcludedFromBackupKey to update the application. How can I guarantee that this is done exactly once and that it is not interrupted? Is moving files from the Documents folder important or can I leave them there? Will the backup status of files change to remove them from existing backups?

I use Swift 2.1.1 and configure iOS 8.0 +.

+7
ios
source share
2 answers

As stated in the technical Q&A, the best bet would be to create a subdirectory in the documents and exclude this subdirectory once.

+2
source share

I do not believe that you can write "do it once and make sure that it is done", because you cannot guarantee that your application will not work while it is running. Of course, you can set the completion flag when you are sure that this is done so that after it is completed you do not need to run it again.

Exclude the directory from the backup, not the individual files. From Xcode:

You can use this property to exclude cache and other application support files that are not needed in the backup. Some operations commonly performed with user documents cause this property to be reset as false; therefore, do not use this property in user documents.

Here is the strategy I used with good results (sorry, its in objective-c - I'm not a fast guy. I hope this gives you an idea):

  - (BOOL)moveAllFiles{ // Searches through the documents directory for all files ending in .data BOOL success = true; NSString *myNewLocation = @"/store/my/files/here/now"; // Get the documents directory NSArray *documentDirectories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentDirectory = [documentDirectories objectAtIndex:0]; // Get all files ending in .data (whatever your file type is) NSArray *dataFilesArray = [NSArray arrayWithArray:[NSBundle pathsForResourcesOfType:@"data" inDirectory:documentDirectory]]; // If you have multiple resource types, use this line once each for each resource type, then append the arrays. // Iterate the found files NSString *fileName = [NSString string]; for (int i=0; i<[dataFilesArray count]; i++) { fileName = [[dataFilesArray objectAtIndex:i] lastPathComponent]; // Move file, set success to false if unsuccessful move if (![[NSFileManager defaultManager] moveItemAtPath:[dataFilesArray objectAtIndex:i] toPath:[myNewLocation stringByAppendingPathComponent:fileName] error:nil]) { success = false; // Something went wrong } } return success; } 

Now use the success value to set the key in the user's default file. Check this key at startup. If this is false (or missing), run this procedure (again).

This example shows the file paths. You can do the same with file urls if you want.

+2
source share

All Articles