Ios 使用嵌套数组的RestKit EntityMapping
我的JSON响应中的实体映射部分有问题 我尝试在我的核心数据模型中为成分创建一个单独的实体,并将mappingForEntityName设置为@“成分”,但没有成功。在我的Dish Core数据实体模型中,我声明了@“ingredientValue”、“ingredientName”等,但在我的.sqlite文件中查找时,它不会填充配料字段 RestKit不会向我抛出任何错误。所以我怀疑这与存储在数组中的成分有关 如您所见,我已经声明了2个RKEntityMapping实例(我不知道这样做是否正确?)。但是我的配料没有退回 JSON如下所示:Ios 使用嵌套数组的RestKit EntityMapping,ios,json,core-data,restkit,Ios,Json,Core Data,Restkit,我的JSON响应中的实体映射部分有问题 我尝试在我的核心数据模型中为成分创建一个单独的实体,并将mappingForEntityName设置为@“成分”,但没有成功。在我的Dish Core数据实体模型中,我声明了@“ingredientValue”、“ingredientName”等,但在我的.sqlite文件中查找时,它不会填充配料字段 RestKit不会向我抛出任何错误。所以我怀疑这与存储在数组中的成分有关 如您所见,我已经声明了2个RKEntityMapping实例(我不知道这样做是否正
{ 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"
}
]
}
]}
D.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
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)”添加了部分日志输出
映射我看不到Dish
实体中定义的Dish
。它应该与成分
有一对多的关系
ingredientMapping
应使用成分
实体
如果您调用dishMapping addPropertyMapping:…
,您应该使用…FromKeyPath:@“配料”…
来匹配即将到来的JSON中的键名。如果这是一个愚蠢的问题,我很抱歉,但我对RestKit和整个entityMapping是新手。但是为什么我要在我的实体中定义菜肴呢?我认为我只需要在我的JSON提要中指定我需要从disks对象中提取的项目?出于某种原因,当我将ingredientMapping
的实体更改为components
时,我得到以下错误实体Dish不符合关键“disks”的键值编码是的,这是连接的。您需要修复列出的所有项目(可能还有其他一些我目前无法理解的错误;-))我的话……它起了作用。虽然解释了为什么会很好:)我将菜肴
作为字符串值添加到我的菜肴实体中,并且配料
实体现在已正确填充。但我必须承认我真的不太明白发生了什么。你能详细说明一下吗?我如何将两者联系起来她是为了这两个实体?谢谢你迄今为止的帮助-克里斯
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>"