【问题标题】:RestKit .20 mapping inner JSON array to parent id in Core DataRestKit .20 将内部 JSON 数组映射到 Core Data 中的父 ID
【发布时间】:2013-09-25 15:00:30
【问题描述】:

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

{
"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"
}

}

我的Entity Mapping和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"
                                              }];

【问题讨论】:

    标签: ios core-data restkit-0.20


    【解决方案1】:

    最新版本的 RestKit 定义了在映射期间可用的 @parent 元数据键。所以你可以添加一个类似@parent.id : @"..."的映射来提取结果id,同时映射幻灯片内容。

    【讨论】:

    • 我是否需要将此幻灯片实体映射为某处的子关系才能访问该“父”元数据键?
    • 不,我不这么认为。
    • 在上面添加了编辑,但仍然无法映射该父 ID,有什么想法我的实现是错误的吗?
    • 你在 master 或 development 分支上使用的是最新版本的 RestKit 吗?您是否为映射打开了跟踪日志记录?尝试在生成错误消息的位置添加断点,以便在映射期间查看可用的数据。
    • 使用 .20.3 主分支 - 记录 RKLogConfigureByName("RestKit/ObjectMapping", RKLogLevelTrace);,将添加断点并查看可用的内容
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-07-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-19
    • 1970-01-01
    相关资源
    最近更新 更多