【发布时间】:2014-04-19 15:47:00
【问题描述】:
我的 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
菜.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为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>"
【问题讨论】:
标签: ios json core-data restkit