Sqlite 轻量级核心数据迁移后发生无法识别的选择器

Sqlite 轻量级核心数据迁移后发生无法识别的选择器,sqlite,core-data,migration,unrecognized-selector,Sqlite,Core Data,Migration,Unrecognized Selector,在iOS上,我的核心数据(SQLite)数据库中新添加的字段上有一个无法识别的选择器。我使用Xcode的编辑器菜单添加了一个新的模型版本,然后验证了新版本是当前版本。我还确保了修改表的.h和.m文件已更新,尽管我是手工完成的(如何为您生成这些文件?)。不过没什么不寻常的,只是一个字符串类型的字段 问题似乎在于,在运行试图引用数据库对象的代码时,轻量级迁移永远不会发生。访问newFieldName的字符串提供: -[MyEntity newFieldName]: unrecognized sele

在iOS上,我的核心数据(SQLite)数据库中新添加的字段上有一个无法识别的选择器。我使用Xcode的编辑器菜单添加了一个新的模型版本,然后验证了新版本是当前版本。我还确保了修改表的.h和.m文件已更新,尽管我是手工完成的(如何为您生成这些文件?)。不过没什么不寻常的,只是一个字符串类型的字段

问题似乎在于,在运行试图引用数据库对象的代码时,轻量级迁移永远不会发生。访问newFieldName的字符串提供:

-[MyEntity newFieldName]: unrecognized selector sent to instance 0x852b510
2014-03-27 13:26:21.734 ASSIST for iPad[41682:c07] *** Terminating app due to 
uncaught exception 'NSInvalidArgumentException', reason: '-[MyEntity newFieldName]:
unrecognized  selector sent to instance 0x852b510'
生成上述错误的代码行是下面for循环中的唯一一行:

DataStoreCoreData *dStore = [[DataStoreCoreData alloc] initWithDataItemDescription:did];

for (MyEntity *myEnt in [dStore objects])
     NSString *name = [myEnt newFieldName];    
如前所述,当我检查SQLite db时,它没有新字段,考虑到错误,这是有意义的。因此,我还逐步执行了应该进行迁移的代码,它似乎工作得很好。成功。如下所示:

NSURL *storeUrl = [NSURL fileURLWithPath: [[self applicationDocumentsDirectory] stringByAppendingPathComponent: @"ASSIST.sqlite"]];

// handle db upgrade
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
                         [NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
                         [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil];

NSError *error = nil;
persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel: [self managedObjectModel]];

if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:options error:&error])
    NSLog(@"\r\n Fail. [error localizedDescription]: %@", [error localizedDescription]);
else
    NSLog(@"\r\n Success");

这是第6次db版本升级。所有这些都是轻量级迁移,没有任何异常问题。上面的代码不应该强制SQLite db反映新的模式吗?我怎样才能做到这一点,或者这里还有其他问题吗?

我终于找到了答案。我随问题提供的代码只是对数据库进行最小更改(轻量级迁移)时需要修改的代码的一部分。创建对象模型时,还需要指定新版本。请注意下面代码中的“MyNewVersion”。您应该更新此参数以反映您创建的新版本,然后选择作为当前模型版本:

NSString *path = [[NSBundle mainBundle] pathForResource:@"MyNewVersion" ofType:@"mom" inDirectory:@"ASSIST.momd"];
NSURL *momURL = [NSURL fileURLWithPath:path];
managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:momURL];