Question

I have "A" parent class inherited from NSManagedObject with a property "AP1" or a relation "AR1".

I have other (non-abstract) classes "B", "C", "D" which inherit from "A" so that each of them also has "AP1" property and "AR1" relationship.

Is there any way to setup just the mapping for the parent class so that I can add it to the descriptor:

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

instead of setting up the relation/property mapping for each child class: "B", "C" and "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]];

In case of property mappings the attribute mappings I can get from:

+ (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];
}

Thank you for comments.

Was it helpful?

Solution

Have you tried reusing the same instance:

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

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

(If strange things happen as a result then you can copy rkm each time you call addPropertyMapping:.


From your comment, you could write a method to do this by calling propertyMappings on A_mapping and then iterating the returned array and calling addPropertyMapping: on each of the other mappings to 'share' the mappings between each of them.

There is no such thing as mapping inheritance. The mapping instances are each linked to different classes so you can't directly copy them, you can only copy some parts that you use to build them.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top