0

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.
8
  • Can't see an obvious fault in the code. Turn on trace logging and see where it mentions comments. Commented Aug 6, 2014 at 8:23
  • @Wain Will do, thanks for the little edit too, oversight Commented Aug 6, 2014 at 8:38
  • @Wain In the stack trace, RKMapperOperation > _mutableMappingInfo - there are two fields, _mutablePropertyMappings (all of the Request mappings) and _mutableRelationshipMappings (comment mappings). The second one returns 2 objects in fact (so I'm assuming it's getting the comments!) although not sure why they aren't carrying across though with the mappingResult... Commented Aug 6, 2014 at 9:11
  • I've confirmed that the comment above stands true, I tried a bunch of varying comment amounts and all of the numbers came through in the stack trace in the mutableMappingInfo branch. This is odd as to why it's not being associated with the array of objects. I've even tried things in the mapping dict such as: @"comments.title": @"title", @"(comments)title": @"title", @"(comments).title": @"title" to no avail. Commented Aug 6, 2014 at 9:23
  • 1
    You should very rarely use the mapping result array (use dictionary instead) and what is the request you assign? I'm not clear how you are testing for comments. Commented Aug 6, 2014 at 22:28

1 Answer 1

1

I found a fix to this issue, and although it might not be everyone's cup of tea, hopefully it can help someone else down the track.

In my Requests NSObject, I changed the mapping from type 'Comment' to 'NSArray':

- @property ( strong, nonatomic ) Comment *comments;
+ @property ( strong, nonatomic ) NSArray *comments;
Sign up to request clarification or add additional context in comments.

1 Comment

Not about cups of tea, that is the correct solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.