Restkit

Restkit

One of the most intimidating things when I started mobile development was trying to figure out how to ingest json from API services. After some playing around with NSURL and JSON kit I thought to myself something like this has to have been done enough times that someone has written a library around it. After some quick google searches I came across "ASIHTTPRequest":http://allseeing-i.com/ASIHTTPRequest/ but the maintainer seems to have dropped supporting this so I kept looking until I came across "RestKit":http://restkit.org . Within 10 minutes I was parsing the JSON from my API server and up and running. The overall steps are simple. Set up your object manager, I set mine up in my Application delegate as for what I was doing I needed the data loaded as soon as the app loads. Also since it is using a single pattern. The same object manager is available through out the app as long as the Application delegate retains the object: self.objectManager = [RKObjectManager managerWithBaseURLString:@"http://gettome.herokuapp.com/"]; [RKObjectManager setSharedManager: self.objectManager]; next set up your object mappings. I put the methods to do the object mappings in the class that it was being mapped to, in my case User.c: +(void) loadRestkitMappings { RKObjectManager *sharedManager = [RKObjectManager sharedManager]; RKObjectMapping *userMapping = [RKObjectMapping mappingForClass: [User class]]; [userMapping mapKeyPath:@"_id" toAttribute:@"userId"]; [userMapping mapKeyPath:@"token" toAttribute:@"token"]; [userMapping mapKeyPath:@"email" toAttribute:@"email"]; [userMapping mapKeyPath:@"first_name" toAttribute:@"firstName"]; [userMapping mapKeyPath:@"last_name" toAttribute:@"lastName"]; [userMapping mapKeyPath:@"current_location.latitude" toAttribute:@"currentPositionLat"]; [userMapping mapKeyPath:@"current_location.longitude" toAttribute:@"currentPositionLng"]; [sharedManager.mappingProvider setObjectMapping:userMapping forKeyPath: @"user"]; [sharedManager.mappingProvider setObjectMapping:userMapping forKeyPath: @"users"]; [sharedManager.mappingProvider setObjectMapping:userMapping forKeyPath: @"friends"]; [Direction loadRestkitMappingsWithUserMapping: userMapping]; } Note in the above code. The direction class has a call here which passes in the user mapping. The reason behind this is the User has many directions so I pass in the user mapping after I map the attributes. Below in the Direction class I map the actual object: +(void) loadRestkitMappingsWithUserMapping: (RKObjectMapping *) userMapping { RKObjectManager *sharedManager = [RKObjectManager sharedManager]; RKObjectMapping *directionMapping = [RKObjectMapping mappingForClass: [Direction class]]; [directionMapping mapKeyPath:@"_id" toAttribute:@"directionId"]; [directionMapping mapKeyPath:@"user" toRelationship:@"user" withMapping:userMapping]; [directionMapping mapKeyPath:@"end_coordinates.x" toAttribute:@"endCoordinateLatitude"]; [directionMapping mapKeyPath:@"end_coordinates.y" toAttribute:@"endCoordinateLongitude"]; [sharedManager.mappingProvider setObjectMapping:directionMapping forKeyPath: @"direction"]; [Route loadRestkitMappingsWithDirectionMapping: directionMapping andUserMapping: userMapping]; } the relative portion above is the mapKeyPath:toRelationship which maps the User object on Direction. set up your RKObjectLoader delegate and the 2 required methods: @interface FriendsViewController : UITableViewController <UITableViewDelegate, RKObjectLoaderDelegate></code> delegate - (void)objectLoader:(RKObjectLoader *)objectLoader didFailWithError:(NSError *)error used to handle any connection errors - (void)objectLoader:(RKObjectLoader*)objectLoader didLoadObjects (NSArray*)objects used to actually retain your objects that have been mapped successfully. Lastly make the request and specify that the calling class is the delegate NSString *userSessionPath = [NSString stringWithFormat: @"/api/v1/users.json?auth_token=%@", currentUser.token]; [sharedManager loadObjectsAtResourcePath: userSessionPath usingBlock: ^(RKObjectLoader *loader) { loader.method= RKRequestMethodGET; loader.delegate = self; }]; That is basically the gist of it. Check out the documentation on "RestKit":http://restkit.org for more details.