0

我有以下 JSON

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

在 CoreData 中,在 User 类(名为 id、name 的属性)中,我有一个名为 userCreator 和 User 类的一对多关系

现在我需要映射 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",
         }...
4

1 回答 1

0

似乎有问题的台词是

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

虽然我不使用 RK,但很明显你没有告诉 userCreatorMapping 什么映射到什么。你只是"id"在两边都给它。

于 2012-11-28T14:37:10.530 回答