danielbeard
danielbeard

Reputation: 9149

Restkit object mapping with multiple root objects

I have a web service that returns a JSON in the following format:

{
"client": {
         "firstName": "Aaron"
         },
"error": {
         "code":""
         }
}

And I am having setting up the mapping in restkit. I keep getting the following error:

Error Domain=org.restkit.RestKit.ErrorDomain Code=1001 "Could not find an object mapping for keyPath: ''" UserInfo=0x5961f0 {=RKObjectMapperKeyPath, NSLocalizedDescription=Could not find an object mapping for keyPath: ''}

Here is how I am setting up the mappings:

    [[RKObjectManager sharedManager].mappingProvider setMapping:[Client objectMapping] forKeyPath:@"client"];
    [[RKObjectManager sharedManager].mappingProvider setMapping:[ErrorObject objectMapping] forKeyPath:@"error"];

Client Mapping:

+(RKObjectMapping*) objectMapping {
   RKObjectMapping* responseObjectMapping = [RKObjectMapping mappingForClass:[Client class]];
   [responseObjectMapping setRootKeyPath:@"client"];
   [responseObjectMapping mapKeyPathsToAttributes:
    @"firstName", @"firstName",
    nil];
   return responseObjectMapping;
}

Error Mapping:

+(RKObjectMapping*) objectMapping {
   RKObjectMapping* responseObjectMapping = [RKObjectMapping mappingForClass:[ErrorObject class]];
   [responseObjectMapping setRootKeyPath:@"error"];
   [responseObjectMapping mapKeyPathsToAttributes:
    @"code", @"code",
    nil];
   return responseObjectMapping;
}

And calling the service like this:

//Send initial load request
[[RKObjectManager sharedManager] loadObjectsAtResourcePath:[@"/getdetails" stringByAppendingQueryParameters:params] usingBlock:^(RKObjectLoader *loader) {
    loader.delegate = self;
    loader.userData = INITIAL_LOAD_REQUEST;
}];

EDIT:

I fixed my problem by following advice below and also moving my mapping from the init method to setting it in the object loader block like this:

    //Send initial load request
[[RKObjectManager sharedManager] loadObjectsAtResourcePath:[@"/getdetails" stringByAppendingQueryParameters:params] usingBlock:^(RKObjectLoader *loader) {
    loader.delegate = self;

    //set mapping explicitly
    RKObjectMappingProvider *provider = [[RKObjectMappingProvider alloc] init];
    [provider setMapping:[Clien objectMapping] forKeyPath:@"client"];
    [provider setMapping:[ErrorObject objectMapping] forKeyPath:@"error"];
    [loader setMappingProvider:provider];

    loader.userData = INITIAL_LOAD_REQUEST;
}];

Upvotes: 2

Views: 1783

Answers (1)

Paul de Lange
Paul de Lange

Reputation: 10633

Remove the lines

[responseObjectMapping setRootKeyPath:@"client"];

and

[responseObjectMapping setRootKeyPath:@"error"];

Upvotes: 2

Related Questions