Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question: Is it possible to addConnectionForRelationship: with a abstract entity relationship and connectedBy: with a child entity's attributeName? #2121

Open
puttin opened this issue Dec 18, 2014 · 2 comments

Comments

@puttin
Copy link

puttin commented Dec 18, 2014

Example:
Article have a author relationship which point to PenName entity.
PenName have a people relationship which point to People abstract entity.
People has two child entity: Boy and Girl.
Boy have a boyID, Girl have a girlID.

Here comes a JSON:

{
    "articleID": 1,
    "boy_pen_name": {
        "11": @"Jack"
    }
}

"11" is the boyID.
"Jack" is the pen name

I can use RKDynamicMapping to ensure "boy_pen_name" is mapping with a boy PenName mapping.
But how to addConnectionForRelationship:connectedBy: for the people relationship of PenName?
Because People abstract entity don't have any ID attribute.


I came up with a workaround:

The workaround is NOT working:

+ [PenName boyPenNameMapping]:
…
mapping.identificationAttributes = @[@"peopleID", @"penName"];
[mapping addAttributeMappingFromKeyOfRepresentationToAttribute:@"peopleID"];
[mapping addAttributeMappingsFromDictionary:@{@"{peopleID}": @"penName"}];
[mapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:nil toKeyPath:@"people" withMapping:[Boy magicMapping]]];
…
+ [Boy magicMapping]:
…
mapping.identificationAttributes = @[@"boyID"];
[mapping addAttributeMappingFromKeyOfRepresentationToAttribute:@"boyID"];
…
@puttin puttin changed the title Question: Is it possible to addConnectionForRelationship: with a abstract entity and connectedBy: with a child entity's attributeName? Question: Is it possible to addConnectionForRelationship: with a abstract entity relationship and connectedBy: with a child entity's attributeName? Dec 18, 2014
@puttin
Copy link
Author

puttin commented Dec 19, 2014

Update workaround with more detail

Why the workaround above not working:

@interface PenName: NSManagedObject
    @property (nonatomic, retain) NSNumber* peopleID;
    @property (nonatomic, retain) NSString* penName;
    @property (nonatomic, retain) NSString* type;
    @property (nonatomic, retain) People* people;
@end

@interface People: NSManagedObject
    @property (nonatomic, retain) NSString* realName;
@end

@interface Boy: People
    @property (nonatomic, retain) NSNumber* boyID;
@end

JSON:

{
    "boy": {
        "1": "Jack"
    }
}

Mappings:

RKEntityMapping* penNameMapping = [RKEntityMapping mappingForEntityForName:@"PenName" inManagedObjectStore:managedObjectStore];
penNameMapping.identificationAttributes = @[ @"peopleID", @"penName" ];
[penNameMapping addAttributeMappingFromKeyOfRepresentationToAttribute:@"peopleID"];
[penNameMapping addAttributeMappingsFromDictionary:@{@"{peopleID}": @"penName"}];
[penNameMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:nil toKeyPath:@"people" withMapping:[Boy boyMapping]]];

+ [Boy boyMapping]:

RKEntityMapping* boyMapping = [RKEntityMapping mappingForEntityForName:@"Boy" inManagedObjectStore:managedObjectStore];
boyMapping.identificationAttributes = @[@"boyID"];
[boyMapping addAttributeMappingFromKeyOfRepresentationToAttribute:@"boyID"];

This will not work because RKObjectContainsValueForMappings(id representation, NSArray *propertyMappings) cannot deal with the RKObjectMappingNestingAttributeKeyName


Tricky workaround:

@implementation Boy (Tricky)
- (NSString *)trickyThing
{
    return nil;
}
- (void)setTrickyThing:(NSString *)trickyThing
{
    //nothing happen here
}
@end

then edit the + [Boy boyMapping] to + [Boy boyMappingWithBoyID:(NSString *)boyIDString]

RKEntityMapping* boyMapping = [RKEntityMapping mappingForEntityForName:@"People" inManagedObjectStore:managedObjectStore];
peopleMapping.identificationAttributes = @[@"boyID"];
[boyMapping addAttributeMappingFromKeyOfRepresentationToAttribute:@"boyID"];
//the tricky ↓
[boyMapping addAttributeMappingsFromDictionary:@{boyIDString: @"trickyThing"}];

Then the dynamic JSON format can be parsed correctly.


The main problem is when some one need to add relationship from the dynamic JSON with such abstract & child entity , the addConnectionForRelationship:connectedBy: is not working

@puttin
Copy link
Author

puttin commented Dec 19, 2014

Summary:
So I find two problems:

  • addConnectionForRelationship:connectedBy: is not working with abstract & child entity
  • RKObjectContainsValueForMappings inside the applyRelationshipMappings will destroy expectation with above case
    • addAttributeMappingFromKeyOfRepresentationToAttribute with dynamic attribute
    • nil sourceKeyPath RKRelationshipMapping with dynamic attribute
    • RKObjectContainsValueForMappings continue the for (RKRelationshipMapping *relationshipMapping in [self relationshipMappings]) inside applyRelationshipMappings without mapping

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant