我正在使用映射模型迁移数据模型。
实体具有名为 deleted 的属性,该属性未迁移,因为Core Data采用 NSManagedObject 的已删除属性而不是我的。< / p>
如何强制映射模型使用我的属性?
我可以在价值表达中使用什么吗?这就是我现在使用的:
谢谢。
答案 0 :(得分:1)
不幸的是你使用了一个保留字(我怀疑当时会发出警告)。
您最好的选择是进行轻量级迁移,该值将不迁移。然后迁移后;迭代数据并按对象手动更新值。您只需要执行此操作一次,因为迁移完成后旧的保留字属性将消失。
答案 1 :(得分:0)
我找到了解决方案:
我实现了一个自定义 MigrationPolicy ,如下所示:
- (BOOL)createDestinationInstancesForSourceInstance:(NSManagedObject *)sourceInstance
entityMapping:(NSEntityMapping *)mapping
manager:(NSMigrationManager *)manager
error:(NSError *__autoreleasing *)error
{
NSManagedObject *newObject = [NSEntityDescription insertNewObjectForEntityForName:[mapping destinationEntityName] inManagedObjectContext:[manager destinationContext]];
// Add the old 'deleted' attribute to a renamed attribute
[newObject setValue:[NSNumber numberWithBool:[((OldEntityModel *)sourceInstance) deleted]] forKey:@"newDeletedAttribute"];
// Add all the other attributes
[newObject setValue:[sourceInstance valueForKey:@"field1"] forKey:@"field1"];
[newObject setValue:[sourceInstance valueForKey:@"field2"] forKey:@"field2"];
// Add the relationships
NSSet *relationshipAttribute = [[NSSet alloc] initWithArray:[manager destinationInstancesForEntityMappingNamed:@"OtherEntityToOtherEntity" sourceInstances:@[[sourceInstance valueForKey:@"relationshipAttribute"]]]];
[newObject relationshipAttribute forKey:@"relationshipAttribute"];
[manager associateSourceInstance:sourceInstance withDestinationInstance:newObject forEntityMapping:mapping];
return YES;
}
将 sourceEntity 投射到旧版本版本允许访问在轻量级迁移或映射模型中无法访问的已删除属性。