22 iphone version-control xcode core-data mapping-model
我开始使用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];
}
Run Code Online (Sandbox Code Playgroud)
这完全正常,通过按下+按钮,新的"我的重复字符串"将被添加到表视图中并处于持久存储中.
然后我在XCode中按下了"设计 - >添加模型版本".我向现有实体添加了三个实体,并向现有"评估"实体添加了新属性.然后,我通过按"文件 - >新文件 - >托管对象类"创建了实体的新文件,并为我的四个实体创建了一个新的.h和.m文件,包括带有Evaluation.h和评估的"评估"实体.M.现在我通过设置"设计 - >数据模型 - >设置当前版本"来更改模型版本.完成所有这些后,我更改了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];
}
Run Code Online (Sandbox Code Playgroud)
这不起作用!每次我想添加一行模拟器崩溃,我得到以下内容:
"NSInternalInconsistencyException', reason: 'This NSPersistentStoreCoordinator has no persistent stores. It cannot perform a save operation.'"
Run Code Online (Sandbox Code Playgroud)
在我知道在更改数据模型上的任何内容之后创建新版本之前我有这个错误,但为什么这仍然会出现?我是否需要进行任何映射(即使我刚刚添加了以前不存在的实体和属性?).在Apple Dev教程中听起来很容易,但是我一直在努力解决这个问题,在改变模型版本之后从未工作过.
han*_*eyp 50
在App Delegate中创建persistentStoreCoordinator时,是否设置了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;
}
Run Code Online (Sandbox Code Playgroud)
Mic*_*ord 30
如果您只是在模拟器中收到此错误,那么您已更改了数据模型,并且它尚未删除您之前使用的sqlite文件.
所以去: ~/Library/Application Support/iPhone Simulator/User/Applications/
然后查看HEX命名文件夹,直到看到您的应用程序.打开Documents目录并删除sqlite文件.错误应该消失.
| 归档时间: |
|
| 查看次数: |
23524 次 |
| 最近记录: |