Techfest 2013 No RESTKit for the Weary

903 views

Published on

This is the deck the accompanied my talk on RESTKit (the RESTful web service framework for iOS) that I gave at Tulsa Techfest 2013.

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
903
On SlideShare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
7
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Techfest 2013 No RESTKit for the Weary

  1. 1. or the Weary No RestKit f Matt Galloway Architactile matt@architactile.com 918-808-3072 Tuesday, January 28, 14
  2. 2. Some Amazingly Cool Data from the “Cloud” ul* Web RESTF es Servic the “Cloud” REST-ish and JSONy. ul,“ of course, I mean * By “RESTF Tuesday, January 28, 14
  3. 3. rvice Calls on iOS Web Se Define your URL NSString *url = @”http://somewebservice.com/objects.json”; Make the Call NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]]; [[NSURLConnection alloc] initWithRequest:request delegate:self]; ese delegate methods!!! Then implement all of th - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error (void)connectionDidFinishLoading:(NSURLConnection *)connection and now you have to parse the JSON in NSData into something useful... Tuesday, January 28, 14
  4. 4. n is great, but... NSJSONSerializatio -(void) parseJSONIntoModel:(NSData *) jsonData { NSError *error = nil; NSDictionary *json = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingAllowFragments error:&error]; if (error!=nil) { // TODO: Insert annoying JSON error handler here. } if ([json isKindOfClass:[NSArray class]]) { //TODO: Uh. I was expecting a dictionary but apparently this is an array. Writng some handling code here. } for(NSString *key in [json allKeys]) { if ([key isEqualToString:@"user"]) { NSDictionary *userDictionary = [json objectForKey:key]; NSNumber *userId = [userDictionary objectForKey:@"id"]; RKGUser *user = [[ObjectFactory sharedFactory] userForUserId:userId]; if (user==nil) { user = [[ObjectFactory sharedFactory] newUser]; user.userId=useriD; } } . . . user.name = [userDictionary objectForKey:@"name"]; user.username = [userDictionary objectForKey:@"username"]; user.phone = [userDictionary objectForKey:@"phone"]; user.email = [userDictionary objectForKey:@"email"]; Tuesday, January 28, 14
  5. 5. nter RestKit E https://github.com/RestK it /RestKit/ ing and modeling ework for consum RestKit is a fram s on iOS and OS X Tful web resource RES Tuesday, January 28, 14
  6. 6. [[RKObjectManager sharedManager] getObjectsAtPath:@"objects.json" parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) { // Call is successful. Objects are all populated. } failure:^(RKObjectRequestOperation *operation, NSError *error) { // Call failed. }]; Tuesday, January 28, 14
  7. 7. RestKit is... rce & available on GitHub * Open Sou * Built on AFNetworking Beautifully multi-threaded * eamlessly with CoreData * Integrates s o works with plain objects * Als lationships, nesting, etc. * Handles re on mapping like an ORM * Based * Very configurable , POST, PATCH, etc. ndles all REST verbs - GET * Ha y actively maintained * Ver ase seeding, search, ch of other stuff - datab * A bu n XML, etc. Tuesday, January 28, 14
  8. 8. ing RestKit Install I <3 CocoaPods $ cd /path/to/MyProject $ touch Podfile $ edit Podfile platform :ios, '5.0' # Or platform :osx, '10.7' pod 'RestKit', '~> 0.20.0' $ pod install $ open MyProject.xcworkspace Tuesday, January 28, 14
  9. 9. Using RestKit: The W e Tuesday, January 28, 14 b Service
  10. 10. Using RestKit: The Object @interface Joke : NSObject @property (nonatomic, strong) NSNumber *jokeId; @property (nonatomic, strong) NSString *text; @end @implementation Joke @end Tuesday, January 28, 14
  11. 11. Using RestKit: The Setup -(void) setupRestKit { RKObjectManager *objectManager = [RKObjectManager managerWithBaseURL:[NSURL URLWithString:@"http://api.icndb.com/"]]; objectManager.requestSerializationMIMEType=RKMIMETypeJSON; [RKObjectManager setSharedManager:objectManager]; . . . eps for CoreData, authentication, etc.) (There are a few extra st Tuesday, January 28, 14
  12. 12. Using RestKit: The Mappin g RKObjectMapping *jokeMapping = [RKObjectMapping mappingForClass:[Joke class]]; [jokeMapping addAttributeMappingsFromDictionary:@{ @"id": @"joke": @"jokeId", @"text"}]; RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:jokeMapping method:RKRequestMethodGET pathPattern:@"jokes" keyPath:@"value" statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)]; [[RKObjectManager sharedManager] addResponseDescriptor:responseDescriptor]; Tuesday, January 28, 14
  13. 13. I <3 Singletons a singleton, so KObjectManager is R apping steps need the setup and m e, usually at app only be done onc launch. it and forget it. :) Se t Tuesday, January 28, 14
  14. 14. Using RestKit: The Call [[RKObjectManager sharedManager] getObjectsAtPath:@"jokes" parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) { self.jokes = mappingResult.array; [self.tableView reloadData]; } failure:^(RKObjectRequestOperation *operation, NSError *error) { [self displayError:error]; }]; RKMappingResult has count,firstObject, array, dictionary, and set properties Tuesday, January 28, 14
  15. 15. Using RestKit: The Mappin gf or POST RKObjectMapping *inverseJokeMapping = [jokeMapping inverseMapping]; RKRequestDescriptor *requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:inverseJokeMapping objectClass:[Joke class] rootKeyPath:@"jokes" method:RKRequestMethodPOST ]; [[RKObjectManager sharedManager] addRequestDescriptor:requestDescriptor]; Tuesday, January 28, 14
  16. 16. it: POSTing Using RestK Joke *aJoke=[[Joke alloc] init]; aJoke.jokeId=@9999; aJoke.text=@"Chuck Norris can find the end of a circle."; [[RKObjectManager sharedManager] postObject:aJoke path:@"jokes" parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult){} failure:^(RKObjectRequestOperation *operation, NSError *error){}]; Tuesday, January 28, 14
  17. 17. ome Demos S Tuesday, January 28, 14
  18. 18. Matt Galloway Architactile matt@architactile.com 918-808-3072 Tuesday, January 28, 14

×