Created
September 13, 2011 23:02
-
-
Save shyam-habarakada/1215427 to your computer and use it in GitHub Desktop.
rest-kit-mapping-options
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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" | |
} | |
} | |
] | |
} |
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]];
}
}];
}
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];
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"];
Greg, that would definitely work. I am still learning the intricacies of KVC, so not sure if that's the correct way ...
Until we come up with a system of automatic mapping, this is probably as good as we'll get, barring some convenience methods later on
Sent from my iPhone.
…On Sep 15, 2011, at 3:50 PM, ***@***.*** wrote:
Greg, that would definitely work. I am still learning the intricacies of KVC, so not sure if that's the correct way ...
##
Reply to this email directly or view it on GitHub:
https://gist.github.com/1215427
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Posted on google groups at http://groups.google.com/group/restkit/browse_thread/thread/1065c5466ae7a2ec