我的JSON响应中的实体映射部分出现问题。

我试图在我的核心数据模型中为“成分”创建一个单独的实体,并将mappingForEntityName设置为@“ Ingredients”,但无济于事。在我的Dish Core Data实体模型中,我声明了@“ ingredientValue”,@“ ingredientName”,依此类推,但是当在我的.sqlite文件中查找时,它不会填充成分字段。

RestKit不会向我抛出任何错误。所以我怀疑这与存储在数组中的成分有关吗?

如您所见,我已经声明了2个RKEntityMapping实例(我不知道这是否是正确的方法吗?)。但是我的食材没有退回。

JSON如下所示:

{ 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)];


编辑为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>"

最佳答案

我没有在您的dishes实体中定义Dish。它应该与Ingredient是一对多关系。

ingredientMapping应该使用Ingredient实体。

在调用dishMapping addPropertyMapping:...的位置,应该使用...FromKeyPath:@"ingredients" ...匹配传入的JSON中的键名。

关于ios - RestKit EntityMapping与嵌套数组,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23172099/

10-13 05:42