0

我在将对象数组映射到核心数据条目时遇到了一个奇怪的问题。

用户实体映射和 objectManager 代码:

RKEntityMapping *objectMapping = [RKEntityMapping mappingForEntityForName:@"User" inManagedObjectStore:[RKObjectManager sharedManager].managedObjectStore];

[objectMapping addAttributeMappingsFromDictionary:@{@"id": @"user_id",
                                                    @"first_name": @"first_name",
                                                    @"middle_name":@"middle_name",
                                                    @"last_name":@"last_name",
                                                    @"phone":@"phone",
                                                    @"job_title":@"job_title",
                                                    @"roles":@"roles",
                                                    @"email":@"email",
                                                    @"created_at":@"created_at",
                                                    @"updated_at":@"updated_at",
                                                    @"avatar_urls":@"avatar_urls",
                                                    @"skills":@"skills",
                                                    @"interests":@"interests",
                                                    @"birth_date":@"birth_date"}];


RKEntityMapping *userMapping = [User entityMapping];

[manager addResponseDescriptor:[RKResponseDescriptor responseDescriptorWithMapping:userMapping
                                                                            method:RKRequestMethodGET
                                                                       pathPattern:@"users/:id"
                                                                           keyPath:nil
                                                                       statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)]];

[manager addResponseDescriptor:[RKResponseDescriptor responseDescriptorWithMapping:userMapping
                                                                            method:RKRequestMethodGET
                                                                       pathPattern:@"users"
                                                                           keyPath:nil
                                                                       statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)]];

这是用户实体数组的简单示例。

{
  User1,
  User2,
  User3
}

查看来自 RestKit 的调试日志,所有内容都映射到正确的值。但是当我查看最终结果时,映射数组具有正确数量的项目,但它们都是 User3。同样,数据库也有 User3。User1 和 User2 永远不会存储在数据库中。

我一直在试图找出我哪里出错了。任何帮助表示赞赏。

更新

 [manager getObjectsAtPath:@"users"
                   parameters:@{@"token":token}
                      success:success failure:failure];
[
{
    "avatar_urls": {
        "large": "",
        "medium": "",
        "small": "",
        "thumb": "",
        "icon": ""
    },
    "id": 631,
    "roles": [
        "company_admin"
    ],
    "created_at": "2013-10-23T15:59:06.256-05:00",
    "updated_at": "2013-10-27T16:51:31.947-05:00",
    "skills": [],
    "interests": [],
    "birth_date": null,
    "phone_message_channel": true,
    "email_message_channel": true,
    "first_name": "User",
    "last_name": "One",
    "email": "",
    "phone": null,
    "job_title": null

},
{
    "avatar_urls": {
        "large": "",
        "medium": "",
        "small": "",
        "thumb": "",
        "icon": ""
    },
    "id": 633,
    "roles": [
        "company_admin"
    ],
    "created_at": "2013-10-23T16:07:46.472-05:00",
    "updated_at": "2013-10-27T16:51:26.148-05:00",
    "skills": [],
    "interests": [],
    "birth_date": null,
    "phone_message_channel": true,
    "email_message_channel": true,
    "first_name": "User",
    "last_name": "two",
    "email": "",
    "phone": "",
    "job_title": ""

},
{
    "avatar_urls": {
        "large": "",
        "medium": "",
        "small": "",
        "thumb": "",
        "icon": ""
    },
    "id": 632,
    "roles": [
        "company_admin"
    ],
    "created_at": "2013-10-23T16:07:12.295-05:00",
    "updated_at": "2013-11-01T20:11:26.607-05:00",
    "skills": [],
    "interests": [],
    "birth_date": "",
    "phone_message_channel": true,
    "email_message_channel": true,
    "first_name": "User",
    "last_name": "Three",
    "email": "",
    "phone": "",
    "job_title": ""
} 

]

重申一下成功块映射结果显示数组中有 3 个项目,并且都是用户 3。我还检查了核心数据并确认它只为用户模型添加/更新了一行。

FIX 自己没有积分来发布答案..所以添加为更新

我忘记为我的用户模型添加标识属性。一旦我将它设置为我的身份,一切都会像魅力一样发挥作用...... =.= 剥夺睡眠是不好的......

4

1 回答 1

0

在上面的代码中,您设置了一个RKEntityMapping名为 objectMapping。然后你设置另一个RKEntityMapping叫做userMapping。您不再引用 objMapping 。

您发布的代码中缺少某些内容,或者从未使用过 objectMapping,并且您没有为您的请求设置映射。

于 2013-11-05T02:07:44.327 回答