从单个实体迁移到具有子实体的抽象父实体,未调用 NSEntityMigrationPolicy
我正在尝试升级当前的应用程序以使用抽象父实体和专门的子实体。 我创建了一个自定义 NSEntityMigrationPolicy,并在映射模型中将自定义策略设置为我的类的名称。
我正在像这样初始化我的持久存储,这应该是相当标准的:
NSError *error=nil;
persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel: [self managedObjectModel]];
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
nil];
if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:options error:&error]) {
NSLog(@"Error adding persistent store : %@",[error description]);
NSAssert(error==nil,[error localizedDescription]);
}
当我运行应用程序时,我收到以下错误:
由于未捕获而终止应用程序 例外 'NSInternalInconsistencyException', 原因:“该操作无法进行 完全的。 (可可错误 134140。)'
[错误 userInfo] 包含“reason=无法找到用于迁移的映射模型”
我已验证数据模型的版本 1 将打开,并且如果我设置 NSInferMappingModelAutomaticallyOption我得到了迁移,尽管我的实体没有正确迁移(如预期)。
我已经验证映射模型(cdm)位于应用程序包中,但不知何故它拒绝找到它。 我还在自定义迁移策略中设置了断点和 NSLog() 语句,无论有或没有 NSInferMappingModelAutomaticallyOption ,它都不会运行。
关于为什么它似乎无法找到映射模型的任何提示?
I'm trying to upgrade my current application to use an abstract parent entity, with specialized sub entities.
I've created a custom NSEntityMigrationPolicy, and in the mapping model I've set the Custom Policy to the name of my class.
I'm initializing my persistent store like this, which should be fairly standard :
NSError *error=nil;
persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel: [self managedObjectModel]];
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
nil];
if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:options error:&error]) {
NSLog(@"Error adding persistent store : %@",[error description]);
NSAssert(error==nil,[error localizedDescription]);
}
When i run the app i get the following error :
Terminating app due to uncaught
exception
'NSInternalInconsistencyException',
reason: 'The operation couldn’t be
completed. (Cocoa error 134140.)'
[error userInfo] contains "reason=Can't find mapping model for migration"
I've verified that version 1 of the data model will open, and if i set NSInferMappingModelAutomaticallyOption i get a migration, although my entities are not migrated correctly (as expected).
I've verified that the mapping model (cdm) is in the application bundle, but somehow it refuses to find it.
I've also set breakpoints and NSLog() statements in the custom migration policy, and none of it runs, with or without NSInferMappingModelAutomaticallyOption
Any hints as to why it seems unable to find the mapping model ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,抽象实体有相当大的惩罚。摘要的所有子级将存储在同一个表中。这将创建一个非常宽的桌子,其中有很多空隙。我建议检查您的数据模型并确保这是您真正想要的。
其次,如果您找不到映射模型,则意味着它与源或目标都不匹配。如果您在创建地图模型后更改了目的地,则它将找不到地图。创建映射模型需要是新模型完成后的最后一步。我什至建议锁定模型。
First, abstract entities have a rather large penalty attached to them. All children of the abstract will be stored in the same table. This will create a very wide table with a lot of voids. I would suggest reviewing your data model and making sure that is what you really want.
Second, if you cannot find the mapping model that means it is not lining up with either the source or the destination. If you changed the destination after creating the mapping model, it won't find the map. Creating a mapping model needs to be the last step after the new model is finished. I would even recommend locking the model.
我似乎已经解决了上述问题,尽管我不知道我做了什么。
在尝试了 4 天让迁移工作后,我最终放弃了,修改了我的设计,然后重新开始。
在这个过程中的某个地方,我将 git 的 master 分支重置为较早的分支以检查一些细节,并且我不小心启动了调试器。
令我惊讶的是,现在迁移运行得很好,一切正常。
我最好的猜测是 XCode 有一些旧的迁移,在清理构建时未能删除它们(即使我已经手动删除了构建目录)
I seem to have resolved the above issue, although I have no clue what i did.
Having tried for 4 days to get the migration working, I finally gave up, scratched my design, and started over.
Somewhere in the process I reset git's master branch to an earlier branch to check up on some details, and I accidentally fired up the debugger.
Much to my surprise, it now ran the migration just fine, and everything worked.
My best guess is that XCode had some old migrations that it failed to delete while cleaning the build (even though i've manually deleted the build directory)