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.
Peter
source share