2012-11-27 112 views
0

我有以下的JSONRestKit直接foregin鍵映射

{ 
    ... 
    "users": [ 
      { 
       "name": "Username 1" 
       "id": "uid1", 
      }, 
      { 
       "name": "Username 2", 
       "id": "uid2",     
       "userCreator": "uid1" 
      }, 
      { 
       "name": "Username 3", 
       "id": "uid3", 
       "userCreator": "uid1" 
      } 
     ] 
} 

而在CoreData,在用戶類(屬性名爲id,名)我有類用戶一個一對多的關係命名userCreator

現在我需要映射JSON的關係,我想

RKManagedObjectMapping * mapping = [RKManagedObjectMapping mappingForClass:[User class] inManagedObjectStore:[RKManagedObjectStore defaultObjectStore]]; 

mapping.primaryKeyAttribute = @"id"; 
[mapping mapAttributes:@"id", @"name", nil]; 

RKManagedObjectMapping * userCreatorMapping = [RKManagedObjectMapping mappingForClass:[User class] inManagedObjectStore:[RKManagedObjectStore defaultObjectStore]]; 
userCreatorMapping.primaryKeyAttribute = @"id"; 
[userCreatorMapping mapAttributes:@"id", nil]; 

//[mapping hasOne:@"userCreator" withMapping:userCreatorMapping]; 
[mapping mapRelationship:@"userCreator" withMapping:userCreatorMapping]; 
[mapping connectRelationship:@"userCreator" withObjectForPrimaryKeyAttribute:@"id"]; 

哪個不工作的權利,生產

... 
"users":[{ 
      "id":"uid1", 
      "name":"Username 1" 
     }, 
     { 
      "id":"uid2", 
      "name":"Username 2", 
      "userCreator":{"id":"uid1"} // wanted "userCreator":"uid1", 
     }... 

回答

0

看來違規線

userCreatorMapping.primaryKeyAttribute = @"id"; 
// ... 
[mapping connectRelationship:@"userCreator" 
    withObjectForPrimaryKeyAttribute:@"id"]; 

雖然我不使用RK,似乎很明顯,你講的不是什麼userCreatorMapping映射到什麼。你只是在兩邊給"id"

+0

我的設置是正確的,我將userCreator中的對象映射到屬性id,它被設置爲主屬性,userCreator中的對象只是一個字符串,它代表了編號 –

+0

然後,您必須將映射完全刪除。然後你會得到字符串。 – Mundi