Skip to content

Instantly share code, notes, and snippets.

@shyam-habarakada
Created September 13, 2011 23:02
Show Gist options
  • Save shyam-habarakada/1215427 to your computer and use it in GitHub Desktop.
Save shyam-habarakada/1215427 to your computer and use it in GitHub Desktop.
rest-kit-mapping-options
/*
* RestKit QUESTION
*
* What is the correct way to map the following server response variants, and
* is there a preferred option?
*
* Also, keeping in mind that these responses can represent nested objects that need to
* be mapped to corresponding object hierarchies on the client side. I can give more
* examples of that if needed.
*
* Finally, the client side is core-data, so the mappings are managed object mappings.
*
*/
// format #1
// named array of objects
// This is the format shown on the RestKit wiki examples
{
persons: [
{
id: 1,
last_name: "Johnson",
first_name: "Stephanie",
gender: "female"
},
{
id: 2,
last_name: "Black",
first_name: "Joe",
gender: "male"
}
]
}
// format #2
// naked array of named objects
[
{
person: {
id: 1,
last_name: "Johnson",
first_name: "Stephanie",
gender: "female"
}
},
{
person: {
id: 2,
last_name: "Black",
first_name: "Joe",
gender: "male"
}
}
]
// format #3
// naked array of (unnamed) objects
[
{
id: 1,
last_name: "Johnson",
first_name: "Stephanie",
gender: "female"
},
{
id: 2,
last_name: "Black",
first_name: "Joe",
gender: "male",
}
]
// format #4
// named array of named objects
{
persons: [
{
person: {
id: 1,
last_name: "Johnson",
first_name: "Stephanie",
gender: "female"
}
},
{
person: {
id: 2,
last_name: "Black",
first_name: "Joe",
gender: "male"
}
}
]
}
@shyam-habarakada
Copy link
Author

@rayfix
Copy link

rayfix commented Sep 14, 2011

One way for #3 (maybe not the best) is to follow the RKTwitter example:

- (void)loadTimeline {
    // Load the object model via RestKit    
    RKObjectManager* objectManager = [RKObjectManager sharedManager];

    [objectManager loadObjectsAtResourcePath:@"" delegate:self block:^(RKObjectLoader* loader) {
        // Handle naked array in JSON, so we instruct the loader to user the appropriate object mapping
        if ([objectManager.acceptMIMEType isEqualToString:RKMIMETypeJSON]) {
            loader.objectMapping = [objectManager.mappingProvider objectMappingForClass:[Person class]];
        }
    }];
}

@shyam-habarakada
Copy link
Author

For #1 above is clearly documented in the RK docs. See the article example at https://github.com/RestKit/RestKit/wiki/Object-mapping

For #2, I was able to get to the actual values with mappings like below - but that seems like a big hack and there must be a better way to do it.

mapKeyPathsToAttributes:@"person.first_name",@"firstName",
     @"person.last_name",@"lastName",
     @"person.gender",@"gender",
     nil];

@grgcombs
Copy link

For #4, maybe try something like this?

RKManagedObjectMapping* personMapping = [RKManagedObjectMapping mappingForClass:[Person class]];
[personMapping mapKeyPath:@"person.first_name" toAttribute:@"firstName"];
[personMapping mapKeyPath:@"person.last_name" toAttribute:@"lastName"];
[personMapping mapKeyPath:@"person.gender" toAttribute:@"gender"];
[personMapping mapKeyPath:@"person.id" toAttribute:@"personID"];
personMapping.primaryKeyAttribute = @"personID";
[[RKObjectManager sharedManager].mappingProvider setMapping:personMapping forKeyPath:@"persons"];

@shyam-habarakada
Copy link
Author

Greg, that would definitely work. I am still learning the intricacies of KVC, so not sure if that's the correct way ...

@grgcombs
Copy link

grgcombs commented Sep 15, 2011 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment