我开始在我的应用程序中使用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];
}
我的问题是,每次我稍微改变一下我的.xcdatamodel时,我就无法获取以前存储在文档中的所有内容,也无法创建任何新实例。事实上,这样做会生成以下异常:
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'This NSPersistentStoreCoordinator has no persistent stores. It cannot perform a save operation.'
我曾认为设置托管文档的“options”属性可以解决问题,但显然这还不够。有人能帮忙吗?我找不到其他确切符合我的需求的问题。