iOS RestKit实体管理器映射对象数组

时间:2013-11-04 10:36:01

标签: ios core-data restkit

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

用户实体映射和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)]];

以下是User Entity数组的一个简单示例。

{
  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 没有积分可以自己发布答案..所以添加为更新

我忘了为我的用户模型添加identificationAttributes。一旦我把它设置为我的身份,一切都像魅力..... =。=睡眠剥夺是坏的....

1 个答案:

答案 0 :(得分:0)

在上面的代码中,您设置了一个名为objectMapping的RKEntityMapping。然后设置另一个名为userMapping的RKEntityMapping。您没有再次引用objetMapping。

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