【问题标题】:NSEntityMigrationPolicy subclass methods not being called未调用 NSEntityMigrationPolicy 子类方法
【发布时间】:2010-09-06 13:25:39
【问题描述】:

我正在尝试从一个 .xcdatamodel 文件迁移到另一个。我有一个 NSEntityMigrationPolicy 子类,我在 xcode-> .xcmappingmodel 文件 -> 实体 -> “自定义策略”字段中输入了它的名称。

我运行我的应用程序,该应用程序成功打开并运行了我的数据的先前版本,因此我只能假设基本迁移已经成功。但是没有调用我的 NSEntityMigrationPolicy 子类方法,以便我可以运行进一步的迁移代码。

@implementation TestMigrationPolicy

- (BOOL)beginEntityMapping:(NSEntityMapping *)mapping manager:(NSMigrationManager *)manager error:(NSError * *)error
{
    NSLog(@"this log is never shown!!!!");
    return YES;
}

有没有人知道为什么我的它可能没有被调用?我是核心数据迁移的新手,我目前不知道为什么它的行为不像我认为的那样。

如果有帮助,我正在创建这样的持久存储..

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]) {

【问题讨论】:

    标签: iphone objective-c cocoa core-data


    【解决方案1】:

    我知道这个问题很老,但这可能对其他人有帮助!

    这是因为您设置了 NSInferMappingModelAutomaticallyOption 选项 - 这意味着正在运行轻量级迁移,而不是使用您的映射模型。删除此选项,将 * NSMigratePersistentStoresAutomaticallyOption* 留在原处,一切都应该正常工作。

    【讨论】:

    • 啊,太好了,谢谢! …但是如果我也想要自动映射呢?所以,我使用的是模型版本 16。我想要从 15 到 16 的手动迁移,但早期的迁移可以是自动的。
    • 当心。对于那些可能遇到这个答案的人,据我所知,它并不完全准确。根据 Apple (developer.apple.com/documentation/coredata/…) 的说法,如果找不到映射,则自动推理是一种回退。问题很可能出在其他地方,因为如果您有一个可以找到的映射,Core Data 应该使用它。
    【解决方案2】:

    我也遇到过同样的问题。就我而言,发生这种情况是因为 Core Data 无法在生成的应用程序包中找到我编译的数据映射文件(扩展名为 'cdm' 的文件)。当我手动将该文件从嵌套包移动到应用程序包的根目录(MyApp.app\NestedBundle.bundle\MyMapping.cdm -> MyApp.app\MyMapping.cdm)时,一切正常。但是这样的文件布局违反了当前应用程序包结构的逻辑,所以我会尝试让 Core Data 也能在嵌套包中看到我的 cdm 文件。

    UPD:似乎最好的解决方案是使用自定义初始化进行迁移过程。可以在这里找到非常好的示例 - http://media.pragprog.com/titles/mzcd/code/ProgressiveMigration/AppDelegate.m。我已采用该代码在所有捆绑包中进行搜索,并且运行良好。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-07-14
      • 1970-01-01
      • 2012-12-15
      • 1970-01-01
      • 1970-01-01
      • 2017-11-09
      • 2011-10-24
      • 2013-07-16
      相关资源
      最近更新 更多