2012-06-29 36 views
0

我們使用RestKit來緩存來自遠程Web服務的數據,類似於Stackoverflows API。RestKit,問題和標籤之間的多對多關係像Stackoverflow api

在API中有問題和標記,但不是在文本中獲取標記,而是獲取標記ID。

的問題,資源是這樣的:

{ 
    "items": [ 
    { 
     "question_id": 11260172, 
     "tags": [ 
      { "tag_id" : 1}, 
      { "tag_id" : 2}, 
      { "tag_id" : 3} 
     ], 
     "view_count": 1, 
     [...] 
} 

的標籤資源看起來是這樣的:

{ 
    "items": [ 
    { 
     "id": 1, 
     "name": "c#", 
    }, 
    { 
     "id": 2, 
     "name": "java", 
    }, 
    { 
     "id": 3, 
     "name": "php", 
    }] 
} 

我們想創建問題和標籤之間的連接表,從而使問題能有很多標籤,標籤有很多問題。

我們有一對多的工作,但不是多對多的連接表。因此,我們想知道RestKit多對多映射應該如何尋找這樣的關係,以及數據模型應該如何看待。

我們嘗試了以下映射,但它不是多對多的。

[tagMapping mapKeyPath:@"id" toRelationship:@"questions" withMapping:tagsQuestionsMappingMapping]; 
[questionMapping mapKeyPath:@"tags" toRelationship:@"tags" withMapping:tagsQuestionsMappingMapping]; 

回答

1

就這樣我們很清楚。因爲您的根密鑰在這兩個不同的Web服務中是相同的,所以您需要使用兩個不同的地圖。

這裏是你的問題的端點一個映射的例子:

RKManagedObjectMapping* nested = [RKManagedObjectMapping mappingForClass: [NSDictionary class] inManagedObjectStore:nil]; 
[nested mapKeyPath: @"tag_id" toAttribute: @"id"]; 

RKManagedObjectMapping* questions = [RKManagedObjectMapping mappingForClass: [NSDictionary class] inManagedObjectStore: nil]; 
[questions mapAttributes: @"question_id", @"view_count", nil]; //I don't know your core data model... 
[questions hasMany: @"tags" withMapping: nested]; 
[questions connectRelationship: @"tags" withObjectForPrimaryKeyAttribute: @"tag_id"]; 

這裏是一個映射的例子爲您標籤端點

RKManagedObjectMapping* tags = [RKManagedObjectMapping mappingForClass: [Tag class] inManagedObjectStore: store]; 
[tags mapAttributes: @"id", @"name", nil]; 

注意:

1)標籤地圖每次都不同,因爲每個Feed中的主鍵屬性名稱不同

2)我不知道你的核心數據地形,所以這些地圖可能不會像現在這樣工作。你將需要了解我正在做什麼,併爲你的應用程序重做它。

3)你從不擔心連接表。這由框架處理。