使用父(抽象)NSObject / NSManagedObject的映射添加RKRelationshipMapping

时间:2014-04-15 17:36:28

标签: objective-c oop restkit restkit-0.20

我有" A"父类继承自NSManagedObject,具有属性" AP1"或关系" AR1"。

我有其他(非抽象)课程" B"," C"," D"继承自" A"所以他们每个人都有#AP; AP1"财产和" AR1"关系。

有没有办法只设置父类的映射,以便我可以将它添加到描述符中:

[A_Mapping addPropertyMapping:
  [RKRelationshipMapping relationshipMappingFromKeyPath:@"AR1"
                                              toKeyPath:@"AR1"
                                            withMapping:AR1_Mapping]];

而不是为每个子类设置关系/属性映射:" B"," C"和" D":

[B_Mapping addPropertyMapping:
  [RKRelationshipMapping relationshipMappingFromKeyPath:@"AR1"
                                              toKeyPath:@"AR1"
                                            withMapping:AR1_Mapping]];
[C_Mapping addPropertyMapping:
  [RKRelationshipMapping relationshipMappingFromKeyPath:@"AR1"
                                              toKeyPath:@"AR1"
                                            withMapping:AR1_Mapping]];
[D_Mapping addPropertyMapping:
  [RKRelationshipMapping relationshipMappingFromKeyPath:@"AR1"
                                              toKeyPath:@"AR1"
                                            withMapping:AR1_Mapping]];

在属性映射的情况下,我可以从以下地址获取属性映射:

+ (NSArray *) attributeMappingsForStore:(RKManagedObjectStore *)managedObjectStore {

    NSString* entityClassName = NSStringFromClass([self class]);

    // Get the list of attributes
    NSManagedObjectContext *managedObjectContext = managedObjectStore.persistentStoreManagedObjectContext;
    NSEntityDescription* entityInfo = [NSEntityDescription entityForName:entityClassName inManagedObjectContext:managedObjectContext];

    return [[entityInfo attributesByName] allKeys];
}

感谢您的评论。

1 个答案:

答案 0 :(得分:0)

您是否尝试过重复使用相同的实例:

RKRelationshipMapping *rkm = [RKRelationshipMapping relationshipMappingFromKeyPath:@"AR1" toKeyPath:@"AR1" withMapping:AR1_Mapping];

[A_Mapping addPropertyMapping:rkm];
[B_Mapping addPropertyMapping:rkm];
...

(如果结果发生了奇怪的事情,那么每次致电copy时都可以rkm addPropertyMapping:


从您的评论中,您可以通过调用propertyMappings上的A_mapping然后迭代返回的数组并在每个其他映射上调用addPropertyMapping:来共享来编写一个方法来执行此操作'他们每个人之间的映射。

没有映射继承这样的东西。映射实例每个都链接到不同的类,因此您无法直接复制它们,您只能复制用于构建它们的一些部分。