2
votes

So I'm trying to map JSON with objects looking like this

{       
    "item_id": "0",
    "product_id": "217",
    "color_id": "55",
    "size_id": "37",        
    "images": [
        {
            "image_url": "http://www.foo.com/bar.jpg"
        }
    ]
},

I map it directly to Core Data entity 'Item'. This entity has one-to-many relationship to entities 'Pictures' called 'images'. Entity 'Picture' has one NSString attribute 'image_url'. Here are my mappings:

- (void) setupPicturesMapping
{
    picturesMapping = [RKEntityMapping mappingForEntityForName:@"Pictures" inManagedObjectStore:self.objectManager.managedObjectStore];

    picturesMapping.identificationAttributes = @[@"image_url"] ;    

    [picturesMapping addAttributeMappingsFromArray:@[@"image_url"]];

    RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:picturesMapping
                                                                                   pathPattern:@"get_pictures"
                                                                                       keyPath:nil
                                                                                   statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
    [self.objectManager addResponseDescriptor:responseDescriptor];
}

- (void) setupItemsMapping
{
itemsMapping = [RKEntityMapping mappingForEntityForName:@"Item" inManagedObjectStore:self.objectManager.managedObjectStore];

itemsMapping.identificationAttributes = @[@"item_id"] ;

[itemsMapping addAttributeMappingsFromArray:@[@"item_id", @"product_id", @"color_id", @"size_id"]];

[itemsMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"images" toKeyPath:@"images" withMapping:picturesMapping]];

RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:itemsMapping
                                                                                   pathPattern:@"get_items"
                                                                                       keyPath:nil
                                                                                   statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
[self.objectManager addResponseDescriptor:responseDescriptor];

}

When I try to load items I get an error: "Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<__NSCFConstantString 0x26f72b4> valueForUndefinedKey:]: this class is not key value coding-compliant for the key image_url.'".

What's the problem?

UPDATE: Sorry, posted wrong code for the first time

1

1 Answers

0
votes

I think you are passing wrong entity name here-

picturesMapping = [RKEntityMapping mappingForEntityForName:@"Pictures" inManagedObjectStore:self.objectManager.managedObjectStore];

Pass "Picture" instead of "Pictures" like-

picturesMapping = [RKEntityMapping mappingForEntityForName:@"Picture" inManagedObjectStore:self.objectManager.managedObjectStore];