我开始使用 Core Data 进行 iPhone 开发。我首先创建一个非常简单的实体(称为Evaluation),只有一个字符串属性(称为evaluationTopic)。我有以下用于插入新字符串的代码:
- (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];
}
这工作得很好,通过按 + 按钮,一个新的“我的重复字符串”将被添加到 TableView 中并持久存储。
然后我在 Xcode 中按“设计 -> 添加模型版本”。我向现有实体添加了三个实体,并向现有“评估”实体添加了新属性。然后,我通过按“文件 -> 新文件 -> 托管对象类”在实体上创建了新文件,并为我的四个实体创建了一个新的 .h 和 .m 文件,其中包括带有Evaluation.h 和Evaluation 的“Evaluation”实体.米现在我通过设置“设计->数据模型->设置当前版本”来更改模型版本。完成所有这些后,我更改了 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];
}
但这不起作用!每次我想添加一行时,模拟器都会崩溃,并得到以下信息:
"NSInternalInconsistencyException', reason: 'This NSPersistentStoreCoordinator has no persistent stores. It cannot perform a save operation.'"
在我知道如何在更改数据模型上的任何内容后创建新版本之前,我遇到了此错误,但为什么仍然出现此错误?我是否需要进行任何映射(即使我只是添加了以前不存在的实体和属性?)。在Apple Dev教程中,这听起来很简单,但我已经为此苦苦挣扎了很长时间,在更改模型版本后从未工作过。
最佳答案
在应用程序委托(delegate)中创建 persistenceStoreCoordinator 时,是否设置了 NSMigratePersistentStoresAutomaticallyOption 和 NSInferMappingModelAutomaticallyOption 选项?
- (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;
}
关于iphone - 在 Xcode 数据模型发生任何更改后,我不断收到 "save operation failure",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1091228/