问题描述
我开始使用Core Data进行iPhone开发。我开始创建一个非常简单的实体(称为评估)只有一个字符串属性(称为evaluationTopic)。我有以下代码插入一个新的字符串:
I started using Core Data for iPhone development. I started out by creating a very simple entity (called Evaluation) with just one string property (called evaluationTopic). I had following code for inserting a fresh string:
- (void)insertNewObject {
// Create a new instance of the entity managed by the fetched results controller.
NSManagedObjectContext *context = [fetchedResultsController managedObjectContext];
NSEntityDescription *entity = [[fetchedResultsController fetchRequest] entity];
NSManagedObject *newManagedObject = [NSEntityDescription insertNewObjectForEntityForName:[entity name] inManagedObjectContext:context];
// If appropriate, configure the new managed object.
[newManagedObject setValue:@"My Repeating String" forKey:@"evaluationTopic"];
// Save the context.
NSError *error;
if (![context save:&error]) {
// Handle the error...
}
[self.tableView reloadData];
}
我的重复字符串将被添加到表视图,并在持久存储。
This worked perfectly fine and by pushing the +button a new "My Repeating String" would be added to the table view and be in persistent store.
然后我按设计 - >添加模型版本在XCode。我向现有实体添加了三个实体,并向现有的评估实体添加了新属性。然后,我通过按下文件 - >新建文件 - >管理对象类创建了一个新的文件,并为我的四个实体创建了一个新的.h和.m文件,包括Evaluation实体与Evaluation.h和评估m。现在我通过设置设计 - >数据模型 - >设置当前版本更改了模型版本。完成所有这一切后,我改变了我的insertMethod:
I then pressed "Design -> Add Model Version" in XCode. I added three entities to the existing entity and also added new properties to the existing "Evaluation" entity. Then, I created new files off the entities by pressing "File -> New File -> Managed Object Classes" and created a new .h and .m file for my four entities, including the "Evaluation" entity with Evaluation.h and Evaluation.m. Now I changed the model version by setting "Design -> Data Model -> Set Current Version". After having done all this, I changed my insertMethod:
- (void)insertNewObject {
// Create a new instance of the entity managed by the fetched results controller.
NSManagedObjectContext *context = [fetchedResultsController managedObjectContext];
NSEntityDescription *entity = [[fetchedResultsController fetchRequest] entity];
Evaluation *evaluation = (Evaluation *) [NSEntityDescription insertNewObjectForEntityForName:[entity name] inManagedObjectContext:context];
// If appropriate, configure the new managed object.
[evaluation setValue:@"My even new string" forKey:@"evaluationSpeechTopic"];
// Save the context.
NSError *error;
if (![context save:&error]) {
// Handle the error...
}
[self.tableView reloadData];
}
每次我想添加一行模拟器崩溃,我得到以下:
This does not work though! Every time I want to add a row the simulator crashes and I get the following:
"NSInternalInconsistencyException', reason: 'This NSPersistentStoreCoordinator has no persistent stores. It cannot perform a save operation.'"
关于在更改数据模型上的任何内容后创建新版本,但为什么这仍然出现?我需要做任何映射(即使我只是添加了以前不存在的实体和属性)。在苹果开发教程中,这听起来很容易,但我一直在努力这个很长一段时间,从来没有改变模型版本后工作。
I had this error before I knew about creating new version after changing anything on the datamodel, but why is this still coming up? Do I need to do any mapping (even though I just added entities and properties that did not exist before?). In the Apple Dev tutorial it sounds very easy but I have been struggling with this for long time, never worked after changing model version.
推荐答案
p>您在应用程序代理中创建persistentStoreCoordinator时是否设置了NSMigratePersistentStoresAutomaticallyOption和NSInferMappingModelAutomaticallyOption选项?
Do you have NSMigratePersistentStoresAutomaticallyOption and NSInferMappingModelAutomaticallyOption options set when you create your persistentStoreCoordinator in the App Delegate?
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {
if (persistentStoreCoordinator != nil) {
return persistentStoreCoordinator;
}
NSURL *storeUrl = [NSURL fileURLWithPath: [[self applicationDocumentsDirectory] stringByAppendingPathComponent: @"database.sqlite"]];
NSError *error = nil;
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
[NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil];
persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel: [self managedObjectModel]];
if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:options error:&error]) {
// Handle error
}
return persistentStoreCoordinator;
}
这篇关于我继续得到“保存操作失败”在我的XCode数据模型的任何更改后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!