使用Restkit进行映射

时间:2013-04-30 06:08:30

标签: objective-c restkit

我使用简单的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];

但我不是要成功建立关系。我的Db总是显示空白关系黑白图像和事件。我试过connectrelationship,但我想我需要图像表的主键来建立连接。任何人都可以指导我纠正这样做。

我是否需要为Images表添加主键,或者是否可以从父表(事件)访问'event_id'并使用它来建立关系。

1 个答案:

答案 0 :(得分:0)

试试这个

    //Note this is Restkit 0.2
    [eventMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"images" toKeyPath:@"images" withMapping: imagesMapping]];