I am having trouble mapping an array of objects (comments) in array of parent objects (requests) from JSON via RestKit's mapping functionality.
All my data returns properly, but for some reason the comment objects are never populated!
See my code below:
request.json:
{
"data": {
"priorityRequests": [
{
"id": 123456,
"title": "Request 1",
"comments": [
{
"author": "John Smith",
"content": "This is a comment"
}, {
"author": "Jane Smith",
"content": "This is another comment"
}
]
}, {
"id": 654321,
"title": "Request 2",
"comments": [
{
"author": "John Smith",
"content": "This is a comment"
}, {
"author": "Jane Smith",
"content": "This is another comment"
}
]
}
]
}
}
Comment.h/m
@interface Comment : NSObject
@property ( strong, nonatomic ) NSString *author;
@property ( strong, nonatomic ) NSString *content;
@end
@implementation Comment
@end
Request.h/m
@import "Request.h"
@interface Request : NSObject
@property ( strong, nonatomic ) NSString *id;
@property ( strong, nonatomic ) NSString *title;
@property ( strong, nonatomic ) Comment *comments;
@end
@implementation Request
@end
RequestManager.m snippet
RKObjectMapping *requestMapping = [ RKObjectMapping mappingForClass: [ Request class ] ];
[ requestMapping addAttributeMappingsFromDictionary:@{
@"id" : @"id",
@"title" : @"versionNumber"
}];
RKObjectMapping *commentMapping = [ RKObjectMapping mappingForClass: [ Comment class ] ];
[ commentMapping addAttributeMappingsFromDictionary:@{
@"title": @"title",
@"author": @"author"
}];
// Failed attempt 1:
[ requestMapping addPropertyMapping: [ RKRelationshipMapping
relationshipMappingFromKeyPath: @"comments"
toKeyPath: @"comments"
withMapping: commentMapping ]
];
// end
// Failed attempt 2:
RKRelationshipMapping* requests_comments = [ RKRelationshipMapping
relationshipMappingFromKeyPath: @"comments"
toKeyPath: @"comments"
withMapping: commentMapping
];
[ requestMapping addPropertyMapping: requests_comments ];
// end
RequestCommunicator.m snippet
NSDictionary *mappingsDictionary = @{ "data.priorityRequest" : requestMapping };
RKMapperOperation *mapper = [ [ RKMapperOperation alloc ]
initWithRepresentation: parsedData // parsed json as above
mappingsDictionary: mappingsDictionary
];
NSError *mappingError = nil;
BOOL isMapped = [ mapper execute: &mappingError ];
// If no errors, returned array of mapped objects
if (isMapped && !mappingError) {
// All data except for comments here
// _comments = (Comment *) nil
[ self.delegate receivedResponseObject: [ mapper mappingResult ].array ];
... etc.