Umair
Umair

Reputation: 398

RestKit one to one relationship mapping error

I am working with Restkit in my app. I have a problem in mapping one to one relationship.

I have two entities Task and TaskNote. Previously there was one to many relationship between Task and TaskNote i.e one task can have many notes.

This code was working fine then

    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]];

But due to some requirement i have to change one to many relationship to one to one between task and its note i.e. one task can have one note. After this change my mapping starts to fail. i.e. when i use NSLog to observe a Task relationship with its Note it turn out nil. and RestKit is logging this error

    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`.}"

The JSON to map is in the form

 "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":"+10001234567",
              "note_username":"Myself",
              "time_stamp":14130583
           }
        ]
     }

Please guide what am i doing wrong here. How to map one to on relationship. After changing model i have also generated new Subclasses of entities Task and TaskNote.

I can give more info if required.

Upvotes: 0

Views: 222

Answers (1)

Adil Soomro
Adil Soomro

Reputation: 37729

The JSON you posted is having taskNote as array, which means, your api didn't change its structure. Its still returning the same array with one object in it. So you've two options:

  1. Recommended: Get taskNote converted to an object from the api. In this case you'll be getting taskNote without square braces []. Which means it won't be an array.
  2. Take taskNote as an array in you xcdatamodel and .h file, map the taskNote as you were already doing and add a helper method to return you the first item of the array as task note.

Have a look at this answer from Wain regarding mapping the first object only here: RestKit 0.22 : how to map only firstObject of an array

Here is another post on google groups titled "map first element from JSON array to single value using keypath mapping", which answer something like this:

There is no way to do this with key-value coding. You will need to map the entire array and then access the first element (perhaps via a convenience method)

Upvotes: 2

Related Questions