restrict sqlite-wal and sqlite-shm from icloud backup

656 views Asked by At

I am working with coredata for the first time and I have to restrict the sqlite db file from iCloud backup which is in documents directory and i have done it using the below code

-(id)init
{
    if((self = [super init]))
    {
        NSURL* documentsDirectoryURL = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
        NSURL* modelURL = [[NSBundle mainBundle] URLForResource:@"Model" withExtension:@"momd"];
        NSURL* giveForwardSqliteURL = [documentsDirectoryURL URLByAppendingPathComponent:@"InfoCollection.sqlite"];
        NSError* error;

        m_managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];    
        m_persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:m_managedObjectModel];

        NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
                             [NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
                             [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil];


        if ([m_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:giveForwardSqliteURL options:options error:&error])
        {
            m_managedObjectContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];
            [m_managedObjectContext setPersistentStoreCoordinator:m_persistentStoreCoordinator];

            [self addSkipBackupAttributeToItemAtPath:giveForwardSqliteURL];
        }
        else
        {
            NSLog(@"Failed to create or open database: %@", [error userInfo]);
            return nil;
        }
    }

    return self;
}

//Prevent iCloud to take backup of documents directory folder

- (BOOL)addSkipBackupAttributeToItemAtPath:(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;
}

Now what i didn't understand is do we also need to restrict sqlite-wal and sqlite-shm files from icloud backup, if yes then how to restrict sqlite-wal and sqlite-shm files from icloud backup

And i want a solution without changing the sqlite db location from documents directory folder... how can we do it

Please correct if anything is wrong in the above code

Thanks in advance

2

There are 2 answers

4
Jody Hagins On

And i want a solution without changing the sqlite db location from documents directory folder... how can we do it

First, make sure you have read this to determine the proper directory in which to place your files.

Second, don't be so tied to putting them directly into the documents directory. In general, it's not a good idea to stuff every file directly into the Documents directory. Instead, you should use a directory hierarchy to logically partition your data.

Next, note that if you exclude a directory from backup, all files in the directory will be excluded as well.

Thus, if you are willing to be somewhat flexible and follow the advice above, you can simply use a subdirectory of the documents directory for your core data database.

NSURL* giveForwardDirURL = [documentsDirectoryURL URLByAppendingPathComponent:@"InfoCollection.dir"];
[[NSFileManager defaultManager] createDirectoryAtURL:giveForwardDirURL
                         withIntermediateDirectories:YES
                                          attributes:nil
                                               error:NULL];
[self addSkipBackupAttributeToItemAtPath:giveForwardDirURL];

NSURL* giveForwardSqliteURL = [giveForwardDirURL URLByAppendingPathComponent:@"InfoCollection.sqlite"];

If, however, you insist on having your files reside in the directory, you will have to search for them at initialization, and then you will have to monitor the directory as the app runs so you can set the flag if/when related files show up.

This thread contains information pertaining to that: Notification of changes to the iPhone's /Documents directory.

2
Aderstedt On

You can force Core Data to use rollback journalling mode, preventing -shl and -wal files, by adding an options dictionary to the -[NSPersistentStoreCoordinator addPersistentStore…] call, like in this answer:

https://stackoverflow.com/a/24261845/453082