2013-04-30 310 views
0

我用簡單的JSON響應映射,映射隨着Restkit

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

這是不相關的Xcode中,不使用'xcode'標籤。 – 2013-04-30 06:09:00

+0

它比'iPhone'更'iOS' – HAS 2013-04-30 06:24:50

+0

如果您是RestKit的新手,爲什麼要使用舊版本? – Wain 2013-04-30 07:31:09

回答

0

試試這個

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