Reputation: 6987
I don't know how to set up this relationship mapping:
@interface Account : NSManagedObject
@property (strong, nonatomic) NSString *name;
@property (strong, nonatomic) NSNumber accountID;
@property (strong, nonatomic) NSSet *sessions;
@end
.
@interface Session : NSManagedObject
@property (strong, nonatomic) NSString *name;
@property (strong, nonatomic) NSNumber sessionID;
@end
JSON I'm trying to map...
{
"userSessions": {
"name" : "Mr. User",
"identification": 54321,
"sessions": [
"418",
"419",
"431",
"441",
"457",
"486",
"504"
]
}
}
Sessions and Account are already object mapped correctly from two other API calls (getSessions and getAccountInfo). I'm having trouble getting the relationship between the two working. The array of numbers represent the sessionID
s.
Here's what I have currently tried (it crashes):
RKEntityMapping *sessionRelationship = [[RKEntityMapping alloc] initWithEntity:sessionEntity];
[sessionRelationship setIdentificationAttributes:@[ @"sessionID" ]];
[sessionRelationship addAttributeMappingFromKeyOfRepresentationToAttribute:@"sessionID"];
RKEntityMapping *accountMapping = [[RKEntityMapping alloc] initWithEntity:accountEntity];
[entityMapping setIdentificationAttributes:@[ @"accountID" ]];
[entityMapping addRelationshipMappingWithSourceKeyPath:@"sessions"
mapping:sessionRelationship];
NSIndexSet *statusCodes = RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful);
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:entityMapping
pathPattern:nil
keyPath:@"userSessions"
statusCodes:statusCodes];
EDIT:
Following Blake's advice I am using a sourceKeyPath of nil, however I'm still getting a crash.
I traced it down the rabbit hole, and the crash happens in
RKValueForAttributeMappingInRepresentation(RKAttributeMapping *, NSDictionary *)
...this is because the dictionary parameter is actually an NSString from the JSON array.
(lldb) po attributeMapping
(RKAttributeMapping *) $26 = 0x076c6a00 <RKAttributeMapping: 0x76c6a00 (null) => rec>
(lldb) po representation
(NSDictionary *) $27 = 0x0e4a20c0 431
(lldb) po [representation class]
(id) $28 = 0x01f9b8e4 __NSCFString
The stack trace can be seen here:
Upvotes: 1
Views: 880
Reputation: 6617
You have stumbled across an unhandled case within RestKit's mapping logic. There was a hand-off between layers of the mapping engine that assumed that the representation being passed was an NSDictionary
, but the caller was not enforcing its part of the contract. I have taken your example JSON and mapping structure and used it to build a unit test in RestKit.
This issue is fixed on the development branch as of https://github.com/RestKit/RestKit/commit/d761de0ea7cfb11851b714e49ff3126d65ab7b49 and will be included in the rc1 version I am going to tag this weekend.
Upvotes: 2