Skip to content

Instantly share code, notes, and snippets.

@interlock
Created June 30, 2012 19:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save interlock/3025112 to your computer and use it in GitHub Desktop.
Save interlock/3025112 to your computer and use it in GitHub Desktop.
RestKit Queue
/* Also called from the AppDelegate */
- (void)configureRestKit {
// Init RestKit and Map Models
RKObjectManager *manager = [RKObjectManager managerWithBaseURLString:@"http://example.com/api"];
manager.client.requestQueue.showsNetworkActivityIndicatorWhenBusy = YES;
[[RKClient sharedClient] setCachePolicy:RKRequestCachePolicyNone];
[[RKClient sharedClient] setValue:[[NSUserDefaults standardUserDefaults] stringForKey:@"UUID"] forHTTPHeaderField:@"App-UUID"];
RKLogConfigureByName("RestKit/Network*", RKLogLevelTrace);
RKLogConfigureByName("RestKit/ObjectMapping", RKLogLevelTrace);
[RKObjectManager sharedManager].acceptMIMEType = RKMIMETypeJSON;
[RKObjectManager sharedManager].serializationMIMEType = RKMIMETypeJSON;
}
/* Called once from the AppDelegate */
- (void)mapping {
RKObjectMapping *objectMapping = [RKObjectMapping mappingForClass:[MKMeeting class]];
[objectMapping mapKeyPath:@"_id" toAttribute:@"identifier"];
[objectMapping mapKeyPath:@"name" toAttribute:@"name"];
[objectMapping mapKeyPath:@"pin" toAttribute:@"pin"];
[objectMapping mapKeyPath:@"hasLocation" toAttribute:@"hasLocation"];
[objectMapping mapKeyPath:@"lat" toAttribute:@"lat"];
[objectMapping mapKeyPath:@"lng" toAttribute:@"lng"];
[[RKObjectManager sharedManager].mappingProvider setObjectMapping:objectMapping forResourcePathPattern:@"/meetings"];
[[RKObjectManager sharedManager].mappingProvider setObjectMapping:objectMapping forResourcePathPattern:@"/meetings/:identifier"];
RKObjectMapping *serialzedMapping = [RKObjectMapping mappingForClass:[NSMutableDictionary class]];
[serialzedMapping mapKeyPath:@"identifier" toAttribute:@"_id"];
[serialzedMapping mapKeyPath:@"name" toAttribute:@"name"];
[serialzedMapping mapKeyPath:@"pin" toAttribute:@"pin"];
[serialzedMapping mapKeyPath:@"hasLocation" toAttribute:@"hasLocation"];
[serialzedMapping mapKeyPath:@"lat" toAttribute:@"lat"];
[serialzedMapping mapKeyPath:@"lng" toAttribute:@"lng"];
[[[RKObjectManager sharedManager] mappingProvider] setSerializationMapping:serialzedMapping forClass:[MKMeeting class]];
RKObjectRouter *router = [RKObjectManager sharedManager].router;
[router routeClass:[MKMeeting class] toResourcePath:@"/meetings/:identifier"];
[router routeClass:[MKMeeting class] toResourcePath:@"/meetings" forMethod:RKRequestMethodGET];
[router routeClass:[MKMeeting class] toResourcePath:@"/meetings" forMethod:RKRequestMethodPOST];
}
/* called in from UI event */
- (void)newMeeting:(NSObject*)sender {
MKMeeting *meeting = [[MKMeeting alloc] init];
meeting.name = [MKRandomNameGenerator randomName];
meeting.pin = [MKRandomNameGenerator randomNumberOfLength:4];
if ( [CLLocationManager locationServicesEnabled] ) {
meeting.hasLocation = YES;
meeting.lat = self.locationManager.location.coordinate.latitude;
meeting.lng = self.locationManager.location.coordinate.longitude;
}
[[RKObjectManager sharedManager] postObject:meeting delegate:self];
}
/* Two methods from the delegate implemented */
- (void)objectLoader:(RKObjectLoader *)objectLoader didLoadObjects:(NSArray *)objects {
MKMeetingViewController *meetingController = [[MKMeetingViewController alloc] initWithNibName:nil bundle:nil];
[meetingController setMeeting: [objects objectAtIndex:0] ];
[self.navigationController pushViewController:meetingController animated:YES];
}
- (void)objectLoader:(RKObjectLoader *)objectLoader didFailWithError:(NSError *)error {
NSLog(@"Error Creating new Meeting: %@", error);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment