2013-11-04 60 views
0

我有一個奇怪的問題,將對象數組映射到核心數據項。iOS 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)]]; 

下面是用戶實體的陣列的簡單例子。

{ 
    User1, 
    User2, 
    User3 
} 

查看來自RestKit的調試日誌,所有內容都映射到正確的值。但是當我查看最終結果時,映射數組的項目數量正確,但它們都是User3。同樣,數據庫也有User3。用戶1和用戶2從不存儲在數據庫中。

我一直在想弄清楚我出錯的地方。任何幫助表示讚賞。

UPDATE

[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我的用戶模型。一旦我將它設置爲我的ID,一切都像魅力..... =。=睡眠剝奪是不好的......

+0

顯示一些真正的JSON以及您如何提出請求。 – Wain

回答

0

在上面的代碼中,您設置了一個名爲objectMapping的RKEntityMapping。然後,您設置另一個名爲userMapping的RKEntityMapping。您不要再次引用objetMapping。

您發佈的代碼中缺少某些內容,或者從未使用過objectMapping,並且您沒有爲您的請求設置映射。

+0

你是對的,我只是把所有的相關信息,而不是整個代碼。但是我找到了修復程序.....我忘記爲我的用戶模型設置identificationAttributes ....多數民衆贊成我得到的不睡覺前一天晚上.. – naqi

+0

根據您使用的RestKit的版本*應*自動設置它,但總是一個好主意,設置identificationAttributes –

+0

我使用最新的一個cocoapods提供,你是對的,它總是最好的安全比對不起。我在一行代碼上浪費了很多時間= / – naqi