0

我正在使用简单的 json 响应进行映射,

{
    "town": "ABC",
    "website": "http://xyz.com",
    "fee": "50.00",
    "event_id": 32,
    "images": [
        {
            "large": "1.jpg",
            "thumbnail": "2.jpg"
        },
        {
            "large": "1.jpg",
            "thumbnail": "2.jpg"
        },
        {
            "large": "1.jpg",
            "thumbnail": "2.jpg"
        }
    ]
}

我的映射功能看起来像这样

RKManagedObjectMapping *eventMapping = [RKManagedObjectMapping mappingForClass:[Events class] inManagedObjectStore:manager.objectStore];
[eventMapping  mapKeyPath:@"town" toAttribute:@"town"];
[eventMapping  mapKeyPath:@"website" toAttribute:@"website"];
[eventMapping  mapKeyPath:@"fee" toAttribute:@"fee"];
[eventMapping  mapKeyPath:@"event_id" toAttribute:@"event_id"];
eventMapping.primaryKeyAttribute = @"event_id";
[manager.mappingProvider setMapping:eventsMapping forKeyPath:@""];

RKManagedObjectMapping *imagesMapping = [RKManagedObjectMapping mappingForClass:[Images class] inManagedObjectStore:manager.objectStore];
[imagesMapping  mapKeyPath:@"large" toAttribute:@"large"];
[imagesMapping  mapKeyPath:@"thumbnail" toAttribute:@"thumbnail"];

[eventsMapping mapKeyPath:@"images" toRelationship:@"images" withMapping:imagesMapping];

但我不会成功地建立关系。我的数据库总是显示空白关系黑白图像和事件。我尝试了连接关系,但我认为我需要图像表的主键才能建立连接。谁能指导我正确的做法。

我是否需要为图像表添加主键,或者是否可以从父表(事件)访问“event_id”并将其用于建立关系。

4

1 回答 1

0

尝试这个

    //Note this is Restkit 0.2
    [eventMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"images" toKeyPath:@"images" withMapping: imagesMapping]];
于 2013-04-30T06:36:25.000 回答