1
votes

I am trying to map and store a response json data using restkit object mapping. unfortunately i cannot able to map a response data. when i view my database the datas are storing, but i get a crash with following message.. this class is not key value coding-compliant for the key

Output response

{
    "users": [
        {
            "id": "123456",
            "ne": "JohnSmith",
            "el": "[email protected]",
            "dob": "1985/02/04",
            "profile": {
                "id": "654321",
                "ht": "170cm",
                "wt": "70kg"
            }
        }
    ]
}

I am using mapping like this

 RKManagedObjectMapping *userProfileMapping = [RKManagedObjectMapping mappingForClass:[GHSUser class] inManagedObjectStore:manager.objectStore];
    [userProfileMapping mapKeyPath:@"id" toAttribute:@"userId"];
    [userProfileMapping mapKeyPath:@"ne" toAttribute:@"name"];    
    [userProfileMapping mapKeyPath:@"el" toAttribute:@"email"];
    [userProfileMapping mapKeyPath:@"dob" toAttribute:@"dob"];

 RKManagedObjectMapping *standardProfileMapping = [RKManagedObjectMapping mappingForClass:[GHSProfile class] inManagedObjectStore:manager.objectStore];

    [standardProfileMapping mapKeyPath:@"id" toAttribute:@"userId"];
    [standardProfileMapping mapKeyPath:@"ht" toAttribute:@"height"];
    [standardProfileMapping mapKeyPath:@"wt" toAttribute:@"weight"];

[manager.mappingProvider setMapping:standardProfileMapping forKeyPath:@"users.profile"];

//here in keypath(users.profile) i am getting crash,but profile details are inserting in db. when i change keypath to profile, i am not getting any crash but profile details are not inserting in db.

  [userProfileMapping mapRelationship:@"users" withMapping:standardProfileMapping];

Error Message:

* Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<_NSObjectID_48_2 0x888a8d0> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key profile.'

1

1 Answers

1
votes

It is because your users parameter is an NSArray, not a NSDictionary. It doesn't know which users.profile to use.

You want to change the same line:

[manager.mappingProvider setMapping:standardProfileMapping forKeyPath:@"users.profile"];

to:

[manager.mappingProvider setMapping:userProfileMapping forKeyPath:@"users"];

and add this line to your userProfileMapping:

[userProfileMapping mapKeyPath: @"profile" toRelationship: @"profile" withMapping: standardProfileMapping]