将coredata文件存储在文档目录之外? [英] Store coredata file outside of documents directory?

查看:172
本文介绍了将coredata文件存储在文档目录之外?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I allow itunes sharing in my application but I noticed that the coredata create sqlite in the document directory

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

can I change the save of this file to library , if yes how

最好考虑

推荐答案

我做了一个小帮手方法,创建一个无法通过itunes访问的路径。数据库将存储在库目录中,这包括在itunes备份中。

this is possible. I made a little helper method which creates a path that is not accessible through itunes. The database will be stored in the library directory, which is included in itunes back ups.

- (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"];



<方法:

in the - (NSPersistentStoreCoordinator *)persistentStoreCoordinator method with:

NSURL *storeURL = [NSURL fileURLWithPath:[[self applicationPrivateDocumentsDirectory] stringByAppendingPathComponent:@"SomeApp.sqlite"]];

这篇关于将coredata文件存储在文档目录之外?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆