2015-03-03 65 views
0

我在我的應用程序中使用Restkit。我在映射一對一關係時遇到問題。RestKit一對一的關係映射錯誤

我有兩個實體任務和TaskNote。以前,Task和TaskNote之間有一對多的關係,即一個任務可以有很多筆記。

此代碼工作正常,然後

NSDictionary *taskNoteObjectMapping = @{ 
             @"noteID" : @"noteID", 
             @"taskID" : @"taskID", 
             @"time_stamp" : @"time_stamp", 
             @"noteText" : @"noteText", 
             @"note_user_phone_no" : @"note_user_phone_no", 
             @"note_username" : @"note_username" 
             }; 

RKEntityMapping *taskNoteEntityMapping = [RKEntityMapping mappingForEntityForName:@"TaskNote" inManagedObjectStore:managedObjectStore]; 
[taskNoteEntityMapping addAttributeMappingsFromDictionary:taskNoteObjectMapping]; 
taskNoteEntityMapping.identificationAttributes = @[@"noteID"]; 


[taskEntityMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"taskNote" toKeyPath:@"taskNote" withMapping:taskNoteEntityMapping]]; 

但由於一些要求我必須任務和它的音符即一個任務之間切換一對多的關係,一個人能有一個音符。在這個改變之後,我的映射開始失敗。即當我使用NSLog觀察與它的任務關係注意它結果爲零。和RestKit被記錄在表格

"tasks":[ 
    { 
     "taskID":248, 
     "listID":388, 
     "taskName":"Cure", 
     "taskSyncStatus":1, 
     "taskState":0, 
     "task_latitude":0, 
     "task_longitude":0, 
     "taskLog":[ ], 
     "taskComment":[ ], 
     "taskNote":[ 
      { 
       "noteID":113, 
       "taskID":248, 
       "noteText":"Note from server", 
       "note_user_phone_no":"+100", 
       "note_username":"Myself", 
       "time_stamp":14130583 
      } 
     ] 
    } 

請指導我在做什麼錯在這裏這個錯誤

Failed transformation of value at keyPath 'taskNote' to representation of type 'TaskNote': Error Domain=org.restkit.RKValueTransformers.ErrorDomain Code=3002 "Failed transformation of value '(
    "<TaskNote: 0x17833a60> (entity: TaskNote; id: 0x17833aa0 <x-coredata:///TaskNote/t33EC8AB0-1E62-4041-8B91-4B57BC0474F74> ; data: {\n noteID = 113;\n noteText = \"Note from server\";\n \"note_user_phone_no\" = \"+923335729486\";\n \"note_username\" = Myself;\n task = nil;\n taskID = 248;\n \"time_stamp\" = 14130583;\n})" 
)' to TaskNote: none of the 2 value transformers consulted were successful." UserInfo=0x166ba240 {detailedErrors=(
    "Error Domain=org.restkit.RKValueTransformers.ErrorDomain Code=3002 \"The given value is not already an instance of 'TaskNote'\" UserInfo=0x16628750 {NSLocalizedDescription=The given value is not already an instance of 'TaskNote'}", 
    "Error Domain=org.restkit.RKValueTransformers.ErrorDomain Code=3000 \"Expected an `inputValue` of type `NSNull`, but got a `__NSArrayM`.\" UserInfo=0x166ba1e0 {NSLocalizedDescription=Expected an `inputValue` of type `NSNull`, but got a `__NSArrayM`.}" 

的JSON映射爲。如何映射一對一的關係。更改模型後,我還生成了實體任務和TaskNote的新子類。

如果需要,我可以提供更多信息。

+0

什麼是類型/類taskNote'場的'現在的.h文件和'xcdatamodel'? – 2015-03-03 09:22:04

+0

@AdilSoomro任務註釋目標實體是xcdatamodel中的TaskNote。和Task.h中一樣,它是TaskNote – Umair 2015-03-03 09:25:46

回答

2

你發佈的JSON有taskNote作爲數組,這意味着你的api沒有改變它的結構。它仍然返回同一個數組和一個對象。所以,你兩個選擇:

  1. 推薦:獲取taskNote轉換爲從API的對象。在這種情況下,你會得到taskNote沒有方括號 []。這意味着它不會是一個數組。
  2. taskNote爲您xcdatamodel數組和.h文件,映射 taskNote因爲你已經在做,並添加輔助方法 回報,你的數組作爲任務注的第一個項目。

有關於這裏只映射第一對象看看這個答案從WainRestKit 0.22 : how to map only firstObject of an array

這裏是名爲"map first element from JSON array to single value using keypath mapping"谷歌組,回答是這樣的另一篇文章:

有沒有辦法用鍵值編碼來做到這一點。您將需要映射 整個陣列,然後訪問第一個元素(可能通過一個 方便的方法)

+0

謝謝,我自己正在考慮解決方案#2,並已實施#2,我將分別測試解決方案1,然後接受答案。 – Umair 2015-03-03 11:52:39