RestKit .20将内部JSON数组映射到Core Data中的父ID

时间:2013-09-25 15:00:31

标签: ios core-data restkit-0.20

这是JSON返回,我正在尝试映射“结果” - >“id”并将其添加到Core Data中的Slides Entity,到目前为止所有内容都是映射我只是不知道如何访问父ID并将其映射到幻灯片实体

{
"result": {
    "id": 47,
    "type": "Slidedeck",
    "name": "Brendan Demo",
    "version": "0.24",
    "slides": [
        {
            "id": 767,
            "label": "",
            "order": 1,
            "notes": "",
            "file": "slide-38.jpg",
            "url": "http://api.testapp.com/v4/resources/767/download/slide",
            "thumb": "http://api.testapp.com/v4/resources/767/download/slide?thumb=true",
            "components": {
                "hotspots": []
            }
        },
        {
            "id": 768,
            "label": "",
            "order": 2,
            "notes": "",
            "file": "slide-54.png",
            "url": "http://api.testapp.com/v4/resources/768/download/slide",
            "thumb": "http://api.testapp.com/v4/resources/768/download/slide?thumb=true",
            "components": {
                "hotspots": []
            }
        }
    ]
},
"status": {
    "code": 200,
    "message": "OK"
}

}

我的实体映射和RKResponsedescriptor如下:

RKEntityMapping *mapping = [RKEntityMapping mappingForEntityForName:@"Slides"
                                               inManagedObjectStore:[RKManagedObjectStore  defaultStore]];


[mapping addAttributeMappingsFromDictionary:@{@"id": @"slideID",
                                              @"label": @"slideLabel",
                                              @"order": @"slideOrder",
                                              @"notes": @"slideNotes",
                                              @"file": @"slideFile",
                                              @"url": @"slideURL",
                                              @"thumb": @"slideThumb"
                                              }];

slidesResponseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:resourcesEntityMapping method:RKRequestMethodAny pathPattern:[NSString stringWithFormat:@"%@%@", VERSION, @"/resources/:slideDeckID"]  keyPath:@"result.slides" statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];

有什么想法吗?我已经省略了我之前完成此操作以保持代码清晰的所有尝试。所以目前在这里没有失败的尝试代码来获取父结果id。

修改 我已经将@parent添加到RKEntityMapping中,如上所述

[mapping addAttributeMappingsFromDictionary:@{@"@parent.id":@"slideDeckID",
                                              @"id": @"slideID",
                                              @"label": @"slideLabel",
                                              @"order": @"slideOrder",
                                              @"notes": @"slideNotes",
                                              @"file": @"slideFile",
                                              @"url": @"slideURL",
                                              @"thumb": @"slideThumb"
                                              }];

也尝试过:

[mapping addAttributeMappingsFromDictionary:@{@"@parent.slide.id":@"slideDeckID",
                                              @"id": @"slideID",
                                              @"label": @"slideLabel",
                                              @"order": @"slideOrder",
                                              @"notes": @"slideNotes",
                                              @"file": @"slideFile",
                                              @"url": @"slideURL",
                                              @"thumb": @"slideThumb"
                                              }];

但我总是收到以下跟踪消息:

未找到mappable属性值keyPath'@ parent.id'

未找到mappable属性值keyPath'@ parent.result.id'

任何想法我做错了。

修改

当我将密钥路径设置为以下内容时,检查了我的日志:

reourcesResponseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:resourcesEntityMapping method:RKRequestMethodGET pathPattern:[NSString stringWithFormat:@"%@%@", VERSION, @"/resources/:slideDeckID"]  keyPath:@"result.slides" statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];

RestKit记录以下内容:

restkit.object_mapping:RKMapperOperation.m:231 Asked to map source object {
components =     {
    hotspots =         (
    );
};
file = "slide-54.png";
id = 768;
label = "";
notes = "";
order = 2;
thumb = "http://api.idetailapp.com/v4/resources/768/download/slide?thumb=true";
url = "http://api.idetailapp.com/v4/resources/768/download/slide";
} with mapping <RKEntityMapping:0x8e30af0 objectClass=Resources propertyMappings=(
"<RKAttributeMapping: 0x8e31b50 thumb => slideThumb>",
"<RKAttributeMapping: 0x8e31a80 @parent.id => slideDeckID>",
"<RKAttributeMapping: 0x8e31aa0 id => slideID>",
"<RKAttributeMapping: 0x8e31ad0 label => slideLabel>",
"<RKAttributeMapping: 0x8e31bb0 order => slideOrder>",
"<RKAttributeMapping: 0x8e31a50 notes => slideNotes>",
"<RKAttributeMapping: 0x8e31c00 file => slideFile>",
"<RKAttributeMapping: 0x8e31cd0 url => slideURL>"
)>

即使我的响应json包含父id元素,它似乎也不在那里解析:

{
result =     {
    id = 47;
    name = "Russell Demo";
    slides =         (
                    {
            components =                 {
                hotspots =                     (
                );
            };
            file = "slide-38.jpg";
            id = 767;
            label = "";
            notes = "";
            order = 1;
            thumb = "http://api.idetailapp.com/v4/resources/767/download/slide?thumb=true";
            url = "http://api.idetailapp.com/v4/resources/767/download/slide";
        },
                    {
            components =                 {
                hotspots =                     (
                );
            };
            file = "slide-54.png";
            id = 768;
            label = "";
            notes = "";
            order = 2;
            thumb = "http://api.idetailapp.com/v4/resources/768/download/slide?thumb=true";
            url = "http://api.idetailapp.com/v4/resources/768/download/slide";
        }
    );
    type = Slidedeck;
    version = "0.24";
};
status =     {
    code = 200;
    message = OK;
};
}

我已经检查了我的RestKit版本,并在使用Development和Master分支时获得了相同的结果。已验证此版本在RKMappingOperation类中提供了@parent。我在这里缺少什么?

修改

在我的幻灯片实体映射中添加了以下关系,但我仍然无法获取容器“结果”数据

RKEntityMapping *mapping = [RKEntityMapping mappingForEntityForName:@"Slides"
                                               inManagedObjectStore:[RKManagedObjectStore  defaultStore]];



[mapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"result"
                                                                        toKeyPath:@"slides"
                                                                      withMapping:mapping]];




[mapping addAttributeMappingsFromDictionary:@{@"@parent.id":@"slideDeckID",
                                              @"id": @"slideID",
                                              @"label": @"slideLabel",
                                              @"order": @"slideOrder",
                                              @"notes": @"slideNotes",
                                              @"file": @"slideFile",
                                              @"url": @"slideURL",
                                              @"thumb": @"slideThumb"
                                              }];

1 个答案:

答案 0 :(得分:0)

最新版本的RestKit定义了映射期间可用的@parent元数据键。因此,您可以添加类似@parent.id : @"..."的映射,以便在映射幻灯片内容时提取结果ID。