UIManagedDocument的迁移问题

mic*_*ita 6 core-data core-data-migration uimanageddocument

我开始在我的应用程序中使用CoreData,遵循斯坦福CS193P关于使用iOS 5的新类UIManagedDocument的课程.方法本身非常简单,但我无法理解如何处理我一直在制作的模型修改.这就是我实例化我的UIManagedDocument对象的方法(在appDelegate中,以便其他所有类都可以使用它):

if (!self.database) {
    NSURL *url=[[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
    url = [url URLByAppendingPathComponent:@"AppName"];

    UIManagedDocument *doc = [[UIManagedDocument alloc] initWithFileURL:url];
    NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
                             [NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
                             [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil];
    doc.persistentStoreOptions = options;
    self.database=doc;
    [doc release];
}
Run Code Online (Sandbox Code Playgroud)

我遇到的问题是,每次我更改我的.xcdatamodel时,我都无法获取以前存储在文档中的所有内容以及创建任何新实例.事实上,这样做会产生以下异常:

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'This NSPersistentStoreCoordinator has no persistent stores.  It cannot perform a save operation.'
Run Code Online (Sandbox Code Playgroud)

我认为设置托管文档的"选项"属性可以解决问题,但显然这还不够.有人可以帮忙吗?无法找到真正符合我的确切需求的其他问题.

ana*_*ang 2

在修改核心数据模型之前,您应该“添加模型版本”。

1. 选择原始模型文件。(例如 YourProject.xcdatamodel)

2.“编辑器”->“添加模型版本...”。然后添加新的模型版本(例如2.0)

3. 您将得到一个新的模型文件。(例如 YourProject 2.0.xcdatamodel)。修改它。

4. 更改当前型号版本。选择顶部 .xcdtatmodel 文件 ->“查看”->“实用程序”->“显示文件检查器”。找到标签“版本化核心数据模型”并选择您要修改的正确版本。

也让我困扰了很长一段时间。希望这个方法可以帮到你^^