Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is NSURLIsExcludedFromBackupKey recursive

Tags:

ios

iphone

backup

I have a huge tree of files and dirs for cache in my document directory.

As recommended, I'm going to use NSURLIsExcludedFromBackupKey for preventing iTunes from saving this data with the app.

Can I use it once on my root directory URL, as

[rootDirectoryURL setResourceValue:[NSNumber numberWithBool:YES] forKey:@"NSURLIsExcludedFromBackupKey" error:&error];

Or will I have to call it for each file?

like image 697
Martin Avatar asked Jan 17 '13 10:01

Martin


2 Answers

Yup, you can pass it a NSURL of the directory you want excluded.

- (BOOL)addSkipBackupAttributeToItemAtURL:(NSURL *)URL
{
    assert([[NSFileManager defaultManager] fileExistsAtPath: [URL path]]);

    NSError *error = nil;
    BOOL success = [URL setResourceValue:[NSNumber numberWithBool: YES]
                                  forKey: NSURLIsExcludedFromBackupKey error: &error];
    if(!success){
        NSLog(@"Error excluding %@ from backup %@", [URL lastPathComponent], error);
    }

    return success;
}

And you can test any files if in doubt using

id flag = nil;
[URL getResourceValue: &flag
               forKey: NSURLIsExcludedFromBackupKey error: &error];
NSLog (@"NSURLIsExcludedFromBackupKey flag value is %@", flag)
like image 92
Rog Avatar answered Nov 07 '22 06:11

Rog


From my tests (on a simulator), it is not recursive, and I've just had an app rejection because of too much data in Documents directory. Each folder directly in the Documents directory is flagged, so I guess the files inside them are not, even on a real device. But I also added new content in the folder, so I may just need to add the flag again.

So I'm using this recursive method now, and I add the flag on each new files added :

- (void) addSkipBackupAttributeToFolder:(NSURL*)folder
{
    [self addSkipBackupAttributeToItemAtURL:folder];

    NSError* error = nil;
    NSArray* folderContent = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:[folder path] error:&error];

    for (NSString* item in folderContent)
    {
        NSString* path = [folder.path stringByAppendingPathComponent:item];
        [self addSkipBackupAttributeToFolder:[NSURL fileURLWithPath:path]];
    }
}
like image 45
Tim Autin Avatar answered Nov 07 '22 07:11

Tim Autin