6

I started using CoreData in my application following Stanford CS193P lessons regarding the use of iOS 5's new class UIManagedDocument. The approach itself is quite straightforward but I can't understand how to deal with model modifications i keep making. This is how I instantiate my UIManagedDocument object (inside the appDelegate, so that every other class can use it):

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];
}

The issue I have is that every time I change even a little bit of my .xcdatamodel, I am unable to get all the content previously stored in the document as well as to create any new instance. As a matter of fact doing this generates the following exception:

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'This NSPersistentStoreCoordinator has no persistent stores.  It cannot perform a save operation.'

I thought setting the "options" property of the managed document would have solved the problem, but apparently this isn't enough. Anyone can help? Couldn't' find other questions that actually fit my precise needs.

4

3 回答 3

2

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

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

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

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

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

也困扰了我很久很久。希望这个方法可以帮到你^^

于 2012-04-02T07:23:17.123 回答
0

您的问题有一个非常简单的解决方案。只需从模拟器或设备中删除应用程序(触摸应用程序图标几秒钟,然后触摸应用程序图标开始摆动时出现的十字)。这样,Xcode 会更新您应用的 UIManagedDocument。

于 2012-07-16T17:56:30.820 回答
0

确保您没有将 NSDocumentDirectory 错误地键入为 NSDocumentationDirectory。

   NSURL *url = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];

NSDocumentDirectory 是对的!

于 2013-07-17T21:21:57.137 回答