Question

I have a problem with entity mapping part of my JSON response.

I've tried to make a seperate entity for Ingredients in my Core Data model and set the mappingForEntityName to @"Ingredients", but to no avail. In my Dish Core Data entity model I have declared the @"ingredientValue", @"ingredientName", and so on, but when looking in my .sqlite file, it doesn't populate the fields for ingredients.

RestKit doesn't throw any errors back at me. So I'm suspecting it has something to do with the ingredients being stored in an array?

As you can see I've declared 2 RKEntityMapping instances (I don't know if this is the correct way of doing it?). But my ingredients are not returned.

The JSON looks like this:

{ dishes: [
 {
    id: 34,
    name: "Pavlova med citroncreme og jordbær",
    header: "En laekker marengs kage til enhver lejlighed",
    howto: "lots of text....",
    image: "/img_path/img.jpg",
    created_at: "2014-04-05T00:25:22.378Z",
    ingredients: [
                    {
                       ingredient_id: 27,
                       ingredient_amount: 3,
                       ingredient_unit: "stk",
                       ingredient_name: "æggehvider"
                     },
                     {
                       ingredient_id: 28,
                       ingredient_amount: 2,
                       ingredient_unit: "dl",
                       ingredient_name: "sukker"
                     }
                 ]
    }
]}

Dish.h:

@interface Dish : NSManagedObject


@property (nonatomic, copy) NSString *dishName;
@property (nonatomic, copy) NSString *dishHeader;
@property (nonatomic, copy) NSString *dishImage;
@property (nonatomic, copy) NSString *dishIngredients;
@property (nonatomic) NSInteger dishID;
@property (nonatomic, copy) NSData *dishMainText;
@property (nonatomic) NSDate *createdAt;

//Ingredients
@property (nonatomic, copy) NSString *ingredientName;
@property (nonatomic, copy) NSString *ingredientUnit;
@property (nonatomic) NSInteger ingredientValue;
@property (nonatomic) NSInteger ingredientID;
@end

Dish.m:

RKEntityMapping *dishMapping = [RKEntityMapping mappingForEntityForName:@"Dish" inManagedObjectStore:managedObjectStore];
    [dishMapping addAttributeMappingsFromDictionary:@{
                                                    @"id":          @"dishID",
                                                    @"name":        @"dishName",
                                                    @"header":      @"dishHeader",
                                                    @"howto":       @"dishMainText",
                                                    @"image":       @"dishImage",
                                                    @"created_at":   @"createdAt"}];


    dishMapping.identificationAttributes = @[ @"dishID" ];

    RKEntityMapping *ingredientMapping = [RKEntityMapping mappingForEntityForName:@"Dish" inManagedObjectStore:managedObjectStore];
    [ingredientMapping addAttributeMappingsFromDictionary:@{
                                                    @"ingredient_amount": @"ingredientValue",
                                                    @"ingredient_unit": @"ingredientUnit",
                                                    @"ingredient_name": @"ingredientName"}];

    [dishMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"dishes" toKeyPath:@"dishes" withMapping:ingredientMapping]];


    RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:dishMapping
                                                                                            method:RKRequestMethodGET
                                                                                       pathPattern:@"/dishes.json"
                                                                                           keyPath:@"dishes"
                                                                                       statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];

EDIT Added part of the log output for RKLogConfigureByName("RestKit/ObjectMapping", RKLogLevelTrace)

with mapping <RKEntityMapping:0x8f69750 objectClass=NSManagedObject propertyMappings=(
    "<RKAttributeMapping: 0x8f6c150 name => dishName>",
    "<RKAttributeMapping: 0x8f6c190 id => dishID>",
    "<RKAttributeMapping: 0x8f6c480 header => dishHeader>",
    "<RKAttributeMapping: 0x8f6c4a0 howto => dishMainText>",
    "<RKAttributeMapping: 0x8f6c520 image => dishImage>",
    "<RKAttributeMapping: 0x8f6c5c0 created_at => createdAt>",
    "<RKRelationshipMapping: 0x8f6d8e0 dishes => dishes>"
Was it helpful?

Solution

I don't see dishes defined in your Dish entity. It should be a to-many relationship to Ingredient.

ingredientMapping should use the Ingredient entity.

Where you call dishMapping addPropertyMapping:..., you should be using ...FromKeyPath:@"ingredients" ... to match the key name in the JSON coming in.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top