Reputation: 7216
I have an API that I'm querying where when I query for a list of organizations I get an array of:
{
id: integer,
name: string
}
But I am also able to get details on each organization which returns a single object like:
{
id: integer,
name: string,
description: text,
visit_count: integer,
image_url: string
}
How would I set up the object(s) in RestKit for this?
Upvotes: 0
Views: 193
Reputation: 5845
You need to setup the following:
An object manager
RKObjectManager *objectManager = [RKObjectManager managerWithBaseURL:[NSURL URLWithString:YOUR HOST NAME]];
objectManager.requestSerializationMIMEType=RKMIMETypeJSON;
A mapping object with all the elements in your object
RKObjectMapping *mapping = [RKObjectMapping mappingForClass:[YOUR CLASS class]];
The mapping for your object
[mapping addAttributeMappingsFromArray:@[@"id",
@"name",
@"description",
@"visit_count",
@"image_url"
]];
A response descriptor
RKResponseDescriptor *responseDesciptor = [RKResponseDescriptor responseDescriptorWithMapping:mapping method:RKRequestMethodAny pathPattern:OBJECT URL keyPath:YOUR KEY PATH statusCodes:nil];
Add the response descriptor to the object manager
[objectManager responseDesciptor];
Now you can hit up the server
[[RKObjectManager sharedManager] postObject:nil path:OBJECT URL parameters:nil
success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
self.variable = [NSMutableArray arrayWithArray: mappingResult.array];
}
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"An Error Has Occurred" message:[error localizedDescription] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alertView show];
}];
Upvotes: 1