Skip to content

Instantly share code, notes, and snippets.

@blakewatters
Created February 2, 2013 05:19
Show Gist options
  • Save blakewatters/4696204 to your computer and use it in GitHub Desktop.
Save blakewatters/4696204 to your computer and use it in GitHub Desktop.
// JSON loaded from '/posts/:postID/tags' looks like:
// [{ "name": "development" }, { "name": "restkit" }]
RKManagedObjectStore *managedObjectStore = [RKManagedObjectStore defaultStore];
RKEntityMapping *tagMapping = [RKEntityMapping mappingForEntityForName:@"Tag" inManagedObjectStore:managedObjectStore];
[tagMapping addAttributeMappingsFromDictionary:@{ @"@metadata.routing.parameters.postID": @"postID", @"name": @"name" }];
[tagMapping addConnectionForRelationship:@"posts" connectedBy:@{ @"postID": @"postID" }];
RKObjectManager *objectManager = [RKObjectManager sharedManager];
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:tagMapping pathPattern:@"/posts/:postID/tags" keyPath:nil statusCodes:[NSIndexSet indexSetWithIndex:200]];
[objectManager addResponseDescriptor:responseDescriptor];
[objectManager.router.routeSet addRoute:[RKRoute routeWithRelationshipName:@"tags" objectClass:[RKPost class] pathPattern:@"/posts/:postID/tags" method:RKRequestMethodGET]];
NSManagedObject *post = [NSEntityDescription insertNewObjectForEntityForName:@"Post" inManagedObjectContext:managedObjectStore.mainQueueManagedObjectContext];
[post setValue:@"The Post" forKey:@"title"];
[post setValue:@(1234) forKey:@"postID"];
[objectManager getObjectsAtPathForRelationship:@"tags" ofObject:post parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *blockMappingResult) {
// Objects loaded from /posts/1234/tags are returned
} failure:nil];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment