在斯坦福大学上了关于使用iOS 5的新类UIManagedDocument的CS193P课程后,我开始在我的应用程序中使用CoreData。这种方法本身非常简单,但我不能理解如何处理我一直在进行的模型修改。这就是我实例化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“属性就可以解决这个问题,但显然这还不够。有人能帮上忙吗?找不到其他真正符合我的需求的问题。
发布于 2012-04-02 15:23:17
在修改核心数据模型之前,您应该“添加模型版本”。
它也困扰了我很长很长一段时间。希望这种方式能帮助你^^
发布于 2012-07-17 01:56:31
你的问题有一个非常简单的解决方案。只需从模拟器或设备中删除应用程序(触摸应用程序图标几秒钟,然后触摸应用程序图标开始摆动时出现的十字)。这样,Xcode就会更新你的应用程序的UIManagedDocument。
发布于 2013-07-18 05:21:57
确保您没有将NSDocumentDirectory错误地键入为NSDocumentationDirectory。
NSURL *url = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];NSDocumentDirectory是对的!
https://stackoverflow.com/questions/9771005
复制相似问题