我正在尝试从一个.xcdatamodel文件迁移到另一个文件。我有一个NSEntityMigrationPolicy子类,我已经在xcode-> .xcmappingmodel file -> entity -> "custom Policy“字段中输入了它的名称。
我运行我的应用程序,它成功地打开并运行了我的数据的前一个版本,所以我只能假设基本迁移已经起作用了。但是,我的NSEntityMigrationPolicy子类方法没有被调用,因此我可以运行进一步的迁移代码。
@implementation TestMigrationPolicy
- (BOOL)beginEntityMapping:(NSEntityMapping *)mapping manager:(NSMigrationManager *)manager error:(NSError * *)error
{
NSLog(@"this log is never shown!!!!");
return YES;
}有没有人知道为什么我的it不会被调用?我是核心数据迁移的新手,目前我对为什么这不是我觉得应该的行为感到困惑。
如果有帮助,我正在创建持久存储,如下所示。
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:YES],
NSMigratePersistentStoresAutomaticallyOption,
[NSNumber numberWithBool:YES],
NSInferMappingModelAutomaticallyOption,
nil];
NSError *error = nil;
persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
NSLog(@"storeUrl %@", storeUrl);
if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:options error:&error]) {发布于 2013-03-26 02:45:12
我知道这个问题有点老了,但这可能会对其他人有所帮助!
这是因为您设置了选项NSInferMappingModelAutomaticallyOption -这意味着正在运行轻量级迁移,而不是使用您的映射模型。删除此选项,保留* NSMigratePersistentStoresAutomaticallyOption*,所有选项都将正常工作。
发布于 2011-02-02 18:57:42
我也遇到过同样的问题。在我的例子中,这是因为Core Data在生成的应用程序包中找不到我编译的数据映射文件(扩展名为“cdm”的文件)。当我手动将该文件从嵌套捆绑包移动到应用程序捆绑包(MyApp.app\NestedBundle.bundle\MyMapping.cdm、->、MyApp.app\MyMapping.cdm)的根目录时,一切工作正常。但是这样的文件布局违反了当前应用程序包结构的逻辑,所以我将尝试使用Core Data来查看嵌套包中的cdm文件。
更新:似乎最好的解决方案是在迁移过程中使用自定义初始化。非常好的例子可以在这里找到- http://media.pragprog.com/titles/mzcd/code/ProgressiveMigration/AppDelegate.m。我已经采用该代码在所有包中进行搜索,它工作得很好。
https://stackoverflow.com/questions/3651805
复制相似问题