使用RestKit 0.20的JSON数组进行CoreData映射?

时间:2013-06-24 14:38:49

标签: ios objective-c restkit restkit-0.20

我有一个JSON如下

{
    "code":0,
    "response":{
        "Cirqit":{
            "Cirqit":{...},
            "Owner":{...},
            "CirqitMembers":[
                {
                    "User":{...},
                    "CirqitMember":{...}
                },
                {
                    "User":{...},
                    "CirqitMember":{...}
                }
            ]
        }
    }
}

我的映射代码如下:

   //User mapping
    RKEntityMapping *userMapping = [RKEntityMapping mappingForEntityForName:@"User" inManagedObjectStore:apiInterface.managedObjectStore];
    NSDictionary *userMappingDict = @{@"id": @"id",@"firstname": @"firstname"};
    [userMapping addAttributeMappingsFromDictionary:userMappingDict];

    //Member Status Mapping
    RKEntityMapping *membStatusMapping = [RKEntityMapping mappingForEntityForName:@"MemberStatusInfo" inManagedObjectStore:apiInterface.managedObjectStore];
    NSDictionary *membStatusMappingDict = @{@"id": @"id",@"user_id": @"user_id"};    [membStatusMapping addAttributeMappingsFromDictionary:membStatusMappingDict];

    //Cirqit Member Mapping
    RKEntityMapping *membMapping = [RKEntityMapping mappingForEntityForName:@"CirqitMember" inManagedObjectStore:apiInterface.managedObjectStore];
    NSDictionary *membMappingDict = @{ @"CirqitMember.id":@"id" };
    [membMapping addAttributeMappingsFromDictionary:membMappingDict];
    [membMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"response.Cirqit.CirqitMembers.User" toKeyPath:@"memberStatus" withMapping:userMapping]];
    [membMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"response.Cirqit.CirqitMembers.CirqitMember" toKeyPath:@"memberDetails" withMapping:membStatusMapping]];
    membMapping.identificationAttributes = @[@"id"];

    //Cirqit Mapping
    RKEntityMapping *cirqitMapping = [RKEntityMapping mappingForEntityForName:@"Cirqit" inManagedObjectStore:apiInterface.managedObjectStore];
    NSDictionary *cirqitMappingDict = @{   @"response.Cirqit.Cirqit.id":@"id",
                                 @"response.Cirqit.Cirqit.allow_invite": @"allow_invite"};    
    [cirqitMapping addAttributeMappingsFromDictionary:cirqitMappingDict];
    [cirqitMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"response.Cirqit.CirqitMembers" toKeyPath:@"cirqitMembers" withMapping:membMapping]];
    cirqitMapping.identificationAttributes = @[@"id"];

    NSIndexSet *statusCodes = RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful);
    RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:cirqitMapping pathPattern:nil keyPath:nil statusCodes:statusCodes];
    operation = [apiInterface getOperationWithRequest:request forResponseDescriptors:@[responseDescriptor]];
    [operation start];

这里的membMapping,cirqitMapping工作正常。但是membStatusMapping,userMapping不起作用。实际上没有映射错误,但没有发生映射。

下面的My Managed对象类:

@interface CirqitMember : NSManagedObject

@property (nonatomic, retain) NSNumber * id;
@property (nonatomic, retain) MemberStatusInfo *memberDetails;
@property (nonatomic, retain) User *memberStatus;

@end

1 个答案:

答案 0 :(得分:2)

当您添加关系映射时,映射中的关键路径应该相对于容器,而不是相对于根。目前你有:

[membMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"response.Cirqit.CirqitMembers.User" toKeyPath:@"memberStatus" withMapping:userMapping]];
[membMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"response.Cirqit.CirqitMembers.CirqitMember" toKeyPath:@"memberDetails" withMapping:membStatusMapping]];

将其更改为:

[membMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"User" toKeyPath:@"memberStatus" withMapping:userMapping]];
[membMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"CirqitMember" toKeyPath:@"memberDetails" withMapping:membStatusMapping]];

这也适用于:

[cirqitMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"response.Cirqit.CirqitMembers" toKeyPath:@"cirqitMembers" withMapping:membMapping]];

但是那个可能因JSON的结构而起作用......