具有某些JSON响应的restKit不能与核心数据管理对象一对一匹配

时间:2014-04-23 10:08:10

标签: ios json core-data restkit restkit-0.20

以下是设置:我们有一个API,它以JSON格式提供数据,并希望使用Restkit映射到两个核心数据对象。对象"产品"和"资源",它们与多对一关系有关。 1个产品可以涉及很多资源。因此,我们有产品:

@class Resource; 
@interface Product : NSManagedObject

@property (nonatomic, retain) NSNumber * productid; 
@property (nonatomic, retain) NSString * descriptiontext; 
@property (nonatomic, retain) NSString * brand;
@property (nonatomic, retain) NSSet *productResource;    
@end

和资源

@class Product;
@interface Resource : NSManagedObject

@property (nonatomic, retain) NSNumber * productid;
@property (nonatomic, retain) NSNumber * resourceid;    
@property (nonatomic, retain) NSString * url;
@end

我们在使用Restkit时遇到问题,我们正在访问的API以下面的JSON格式提供其数据,这些格式并不直接与Core Data对象相对应。因此,对于每个产品,API都会返回相关资源,如下所示:

listproducts: [ 
   { productid: 2, 
     description: "description of product2", 
     brand: "brand 2", 
     resource: [ { resourceid: 22, url: "img_3786.jpg" }, 
                 { resourceid: 23, url: "tpa-plated-dessert.jpg" } ],
   }, 
   { productid: 3, 
     description: "description of product3", 
     brand: "brand 3", 
     resource: [ { resourceid: 15, url: "img_3786.jpg" } ],
   }, 
]

我们知道如何使用Restkit正确映射到核心数据对象,如果API分别提供产品和资源。在这种情况下,我们可以使用:

RKEntityMapping *productMapping = [RKEntityMapping mappingForEntityForName:@"Product" inManagedObjectStore:managedObjectStore];
productMapping.identificationAttributes = @[ @"productid" ];
[productMapping addAttributeMappingsFromDictionary:@{
                                                     @"productid"     : @"productid",
                                                     @"description"   : @"descriptiontext",
                                                     @"brand"         : @"brand"                            
                                                     }];

RKEntityMapping *resourceMapping = [RKEntityMapping mappingForEntityForName:@"Resource" inManagedObjectStore:managedObjectStore];
    resourceMapping.identificationAttributes = @[ @"resourceid" ];
    [resourceMapping addAttributeMappingsFromDictionary:@{
                                                          @"resourceid"     : @"resourceid",
                                                          @"productid"      : @"productid",
                                                          @"url"            : @"url",
                                                          }];

[productMapping addConnectionForRelationship:@"productResource" connectedBy:@"productid" ];

但是,我们拥有的API是嵌套的,已经完成了一些" work"通过提供其产品与产品匹配的适当资源,因此[productMapping addConnectionForRelationship:@"productResource" connectedBy:@"productid" ];不合适。我们应该尝试别的东西,比如

[productMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"listproducts.resource" toKeyPath:@"listproducts" withMapping:resourceMapping]];

https://github.com/RestKit/RestKit/wiki/Object-mapping给出了嵌套JSON的示例,但这是一个不同的情况。在这里,资源是嵌套的,没有明确的productid,但是从父代productid获取..我们如何在这个API中使用Restkit?

换句话说,我们希望正确填充NSSet productResource,并填充相应的Resource对象,但当前NSSet为空。

以下是响应描述符:

RKResponseDescriptor *productRD = [RKResponseDescriptor responseDescriptorWithMapping:productMapping
                                                                               method:RKRequestMethodGET
                                                                          pathPattern:nil
                                                                              keyPath:@"listproducts"
                                                                          statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
RKResponseDescriptor *resourceRD = [RKResponseDescriptor responseDescriptorWithMapping:resourceMapping
                                                                               method:RKRequestMethodGET
                                                                          pathPattern:nil
                                                                              keyPath:@"resource"
                                                                          statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
[objectManager addResponseDescriptorsFromArray:@[productRD, resourceRD]];

1 个答案:

答案 0 :(得分:1)

  

我们应该尝试别的东西吗,比如

[productMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"listproducts.resource" toKeyPath:@"listproducts" withMapping:resourceMapping]];

是的,这正是你应该尝试的。除非您已经加入listproducts,因此您的关键路径应为resource。并且关键路径是productResource,基于您的实体子类。

更正上述内容,将填充关系。在使用外键映射时,您只需要显式的id存储,而不是在嵌套数据时(因为嵌套描述了如何连接关系的上下文)。