Reputation: 2344
I need to map a nested array with RestKit and I've almost got it (I think).
The JSON I need to map, looks like this
[
{
_id: "5058670183970a0002000450",
app_store_url: "http://itunes.apple.com/app/culturonda-alto-adige-sudtirol/id534467629",
comments: [
{
_id: "5058670783970a0002000456",
},
{
_id: "50588d7f83970a000200065c",
}
]
}
]
My controllers to store the info looks like this:
#import <Foundation/Foundation.h>
#import "CommentData.h"
@interface DesignData : NSObject
@property (retain, nonatomic) NSString *designId;
@property (retain, nonatomic) NSSet *commentsRelationship;
@end
And:
#import <Foundation/Foundation.h>
@interface CommentData : NSObject
@property (retain, nonatomic) NSString *commentId;
@end
My mapping looks like this:
RKObjectManager *objectManager = [RKObjectManager sharedManager];
RKObjectMapping *commentsMapping = [RKObjectMapping mappingForClass:[CommentData class]];
[commentsMapping mapKeyPathsToAttributes:@"_id", @"commentId", nil];
RKObjectMapping *designMapping = [RKObjectMapping mappingForClass:[DesignData class] ];
[designMapping mapKeyPathsToAttributes:@"_id", @"designId", nil];
[designMapping mapKeyPath:@"comments" toRelationship:@"commentsRelationship" withMapping:commentsMapping];
[objectManager.mappingProvider setMapping:designMapping forKeyPath:@""];
Comments gets into an array, but it doesn't get stored in the comments controller. If I display the comments in the objecloader callback like this:
DesignData *designData = [objects objectAtIndex:0];
NSLog(@"Loaded %@ ", designData.commentsRelationship);
it gives me:
2012-10-20 22:37:55.258 RestKitTest5[4144:c07] Loaded {(
<CommentData: 0x8689730>,
<CommentData: 0x868bed0>,
<CommentData: 0x868bfe0>,
<CommentData: 0x868bf50>
)}
Which is a start. But I can't do:
DesignData *designData = [objects objectAtIndex:0];
NSLog(@"Loaded %@ ", designData.commentsRelationship.commentId);
So how do I store the whole thing as an array?
Upvotes: 4
Views: 1763
Reputation: 593
I worked for me after I replaced this line:
[designMapping mapKeyPath:@"comments" toRelationship:@"commentsRelationship" withMapping:commentsMapping];
With:
[designMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"comments" toKeyPath:@"commentsRelationship" withMapping: commentsMapping]];
Upvotes: 2
Reputation: 2344
I was already doing it right, I was just being stupid in the end.
I got the array and the objects in the array was of the right kind (CommentData), so all I had to do was to loop through the array and from there I could work with each object separately.
Upvotes: 1