0

这是 JSON 返回,我正在尝试映射“结果”->“id”并将其添加到核心数据中的幻灯片实体,到目前为止一切都是映射我只是不知道如何访问该父 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"
                                              }];

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

未找到可映射的属性值 keyPath '@parent.id'

或者

未找到可映射的属性值 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"
                                              }];
4

1 回答 1

0

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

于 2013-09-25T15:13:49.093 回答