Store coredata file outside of document directory?

I allow the use of itunes in my application, but I noticed that coredata creates sqlite in the document directory

Can I change the storage of this file in the library, if so, how

Regards

+2
objective-c iphone core-data
Mar 09 2018-11-11T00:
source share
1 answer

it is possible. I made a small helper method that creates a path inaccessible via itunes. The database will be stored in the library directory, which is included in itunes backups.

- (NSString *)applicationPrivateDocumentsDirectory { static NSString *path; if (!path) { NSString *libraryPath = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) lastObject]; path = [libraryPath stringByAppendingPathComponent:@"Private Documents"]; BOOL isDirectory; if (![[NSFileManager defaultManager] fileExistsAtPath:path isDirectory:&isDirectory]) { NSError *error = nil; if (![[NSFileManager defaultManager] createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:&error]) { NSLog(@"Can't create directory %@ [%@]", path, error); abort(); // replace with proper error handling } } else if (!isDirectory) { NSLog(@"Path %@ exists but is no directory", path); abort(); // replace with error handling } } return path; } 

and then you replace

 NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"SomeApp.sqlite"]; 

in the method - (NSPersistentStoreCoordinator *)persistentStoreCoordinator using:

 NSURL *storeURL = [NSURL fileURLWithPath:[[self applicationPrivateDocumentsDirectory] stringByAppendingPathComponent:@"SomeApp.sqlite"]]; 
+6
Mar 09 2018-11-11T00:
source share
โ€” -



All Articles