将coredata文件存储在文档目录之外?

时间:2011-03-09 09:46:18

标签: iphone objective-c core-data

我允许在我的应用程序中共享itunes,但我注意到coredata在文档目录中创建了sqlite

我可以将此文件的保存更改为库,如果是,如何

最好的问候

1 个答案:

答案 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"]];