属性映射未正确关联。为什么?

时间:2014-08-17 22:22:22

标签: ios objective-c core-data ios7 core-data-migration

编辑1 虽然我理解对于这个特定场景(和其他类似的场景)我可以单独使用映射编辑器来正确迁移我的存储,以便持久存储中的值不会跳转,但这不是我当前问题的解决方案,而只是避免解决问题的根源。我热衷于坚持使用自定义迁移策略,因为这样可以让我在迁移过程中获得很多控制权,特别是对于未来的设置自定义迁移策略对我来说很有意义。这是一个长期解决方案,而不仅仅是针对这种情况。

我建议您尝试帮我解决当前的情况,而不是将我转移到轻量级迁移或建议我避免使用迁移策略。谢谢。

我真的很期待对此问题进行整理,并就我能解决这个问题的方法提出宝贵意见。

我做了什么: 我设置了迁移策略,以便可以将源数据从核心模型的version 1复制到目标数据中{.1}}。

这是迁移政策:

version 2

因此,即使测试的属性在运行时显示正确的值,保存在数据模型存储中的结果值也不正确,如快照中所示。

以下是数据存储的版本1到版本2的比较。

版本1:正确 enter image description here

到版本2:现在正在错误地存储值。

Wrong values being saved into the database in the wrong fields

预期输出应将产品价格插入- (BOOL)createDestinationInstancesForSourceInstance:(NSManagedObject *)sInstance entityMapping:(NSEntityMapping *)mapping manager:(NSMigrationManager *)manager error:(NSError **)error { // Create the product managed object NSManagedObject *newObject = [NSEntityDescription insertNewObjectForEntityForName:[mapping destinationEntityName] inManagedObjectContext:[manager destinationContext]]; NSString *productCode = [sInstance valueForKey:@"productCode"]; NSNumber *productPrice = [sInstance valueForKey:@"productPrice"]; [newObject setValue:productCode forKey:@"productCode"]; [newObject setValue:productPrice forKey:@"productPrice"]; //This is the field where the name has changed as well as the type. [newObject setValue:[NSNumber numberWithBool:YES] forKey:@"productPriceNeedsUpdating"]; // Set up the association between the old source product and the new destination Product for the migration manager [manager associateSourceInstance:sInstance withDestinationInstance:newObject forEntityMapping:mapping]; /* A test statement to make sure the destination object contains the correct values int he right properties: Product description: <NSManagedObject: 0xb983780> (entity: Product; id: 0xb9837b0 <x-coredata:///Product/t97685A9D-09B4-475F-BDE3-BC9176454AEF6> ; data: { productCode = 9999; productPrice = "2.09"; productPriceNeedsUpdating = 1; }) */ // Set up the association between the old source product and the new destination Product for the migration manager return YES; } 字段,而不应插入productPrice字段中,该字段实际上只应具有布尔值。 任何人都可以帮我理解我做错了什么,或者解释一下这里发生了什么?

更新1 - 这是我的ProductPriceNeedsUpdating

enter code here

更新2 - 20 / aug / 2014 01:02 GMT

当我从版本1中删除entity mappings类型的属性ProductPriceLastUpdated时,删除版本2中类型date的属性ProductPriceNeedsUpdate,只留下两个属性两者都匹配版本1和2,然后一切正常。即使我可以将它留在这里继续前进,我也无法忽略当前正在使用数据库版本1的用户,该版本具有无意义的boolean属性,我需要将其转换为布尔值并且名称已更改到ProductPriceLastUpdated。多数事情,当事情开始变得奇怪,价格值显示在ProductPriceNeedsUpdate字段而不是ProductPriceNeedsUpdate字段。

我希望有人可以解决原始问题并告诉我为什么实体映射或更多属性映射未正确保存?

更新3 - EntityMapping和属性:

版本1 enter image description here

第2版 enter image description here

有什么想法吗?

1 个答案:

答案 0 :(得分:5)

首先,如果您只想使用轻量级迁移(在这种情况下您确实应该这样做),您可以摆脱自定义迁移策略。在这种情况下,它不是必需的。而且,事实上,您也可以摆脱自定义映射模型。您需要做的就是在第2版模型中,选择productPriceNeedsUpdating布尔标志,在右侧的属性详细信息检查器中,将默认值设置为YES。这将实现您尝试使用自定义迁移策略实现的目标。

但是,如果您确实需要在自定义迁移策略的代码中编写此代码,我仍然不会使用自定义代码。您只需使用映射模型即可实现此迁移。只需选择ProductToProduct映射,然后在productNeedsUpdating的值表达式中输入YES1

修改

因此,经过相当冗长的屏幕分享后,确定迁移使用的是Marcus Zarra的核心数据书中描述的逐步迁移商店的代码。写入时,WAL模式不是Core Data的默认模式。启用WAL模式时,逐步迁移存储不能正常运行,因为还有两个文件需要处理,即“预先写入日志”和“共享内存”文件。当简单地用新的商店替换旧商店时,如果没有先删除这些文件,就会发生奇怪的事情,如本文所述。最终解决方案最终是为逐步迁移的场景禁用WAL模式,因此首先不会生成额外的文件。