我允许在我的应用程序中共享itunes,但我注意到coredata在文档目录中创建了sqlite
我可以将此文件的保存更改为库,如果是,如何
最好的问候
答案 0 :(得分:6)
这是可能的。我做了一个小帮手方法,创建了一个无法通过itunes访问的路径。数据库将存储在库目录中,该目录包含在itunes备份中。
- (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;
}
然后你会替换
NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"SomeApp.sqlite"];
在- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
方法中使用:
NSURL *storeURL = [NSURL fileURLWithPath:[[self applicationPrivateDocumentsDirectory] stringByAppendingPathComponent:@"SomeApp.sqlite"]];