RestKit仅映射到属性

时间:2013-07-23 09:44:35

标签: ios objective-c core-data restkit-0.20

我的回复如下JSON

 {
   response : {
        data : {
             subjects : [
                {
                },
                {
                }
             ]
        }
   }
  }

我有一个NSManagedObject如下:

@interface Department : NSManagedObject

@property (nonatomic, retain) NSNumber * id;
@property (nonatomic, retain) NSString * name;
@property (nonatomic, retain) NSSet *subjects;

@end

我在Department表中有一个部门记录,id = 1,name =“Physics”。我想从响应JSON数据中修改已获得subjects的现有部门。主题字段只有更新休息应保持相同。你能告诉我RestKit 0.20的映射代码吗?

更新

这是我执行的映射:

NSDictionary *subMappingDict = @{
    @"id": @"id",
    @"sub_name": @"name"
};

RKEntityMapping *subMapping = [RKEntityMapping mappingForEntityForName:@"Subject" inManagedObjectStore:managedObjectStore];
[subMapping addAttributeMappingsFromDictionary:subMappingDict];
subMapping.identificationAttributes = @[@"id"];

RKEntityMapping *deptMapping = [RKEntityMapping mappingForEntityForName:@"Department" inManagedObjectStore:managedObjectStore];
[deptMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"subjects" toKeyPath:@"subjects" withMapping:subMapping]];

RKEntityMapping *collegeMapping = [RKEntityMapping mappingForEntityForName:@"College" inManagedObjectStore:managedObjectStore];
[collegeMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"data" toKeyPath:@"department" withMapping:deptMapping]];

NSIndexSet *statusCodes = RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful);
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:collegeMapping pathPattern:nil keyPath:@"response" statusCodes:statusCodes];

我有一条现有的College记录,其中包含现有的Department条记录。在这里,我只想通过映射更新Department的subjects字段。

0 个答案:

没有答案