Limit sqlite-wal and sqlite-shm from icloud backup

I am working with coredata for the first time, and I need to limit the sqlite db file from the iCloud backup located in the documents directory, and I did this using the code below

-(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 from backing up the document catalog 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 that I did not understand, we also need to limit the sqlite-wal and sqlite-shm files from the icloud backup, if so, how to limit the sqlite-wal and sqlite-shm files from the icloud backup

And I want a solution without changing the location of db sqlite from the document directory folder ... how can we do this

Please correct if something is wrong in the above code.

Thanks in advance

+7
ios sqlite core-data icloud
source share
2 answers

And I want a solution without changing the location of the sqlite database from the document directory folder ... how can we do this

First, make sure you read this to determine the appropriate directory for your files.

Secondly, do not bind them directly to the document folder. In general, it is not recommended that you write each file directly to the Documents directory. Instead, you should use a directory hierarchy to logically split your data.

Then note that if you exclude the directory from the backup, all files in the directory will also be excluded.

Thus, if you are ready to be somewhat flexible and follow the advice given above, you can simply use the document directory subdirectory for your main 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 that your files be in the directory, you will have to look for them during initialization, and then you will need to monitor the directory when the application starts, so you can set the if / when flag for the associated files.

This thread contains information related to this: Notification of changes to the iPhone / Documents directory .

+1
source share

You can force Core Data to use lazy log mode by preventing -shl and -wal files by adding a parameter dictionary to the call -[NSPersistentStoreCoordinator addPersistentStore…] , as in this answer:

fooobar.com/questions/543866 / ...

-one
source share

All Articles