techfest 2013 no restkit for the weary

18
No RestKit for the Weary Matt Galloway Architactile [email protected] 918-808-3072 Tuesday, January 28, 14

Upload: matt-galloway

Post on 06-May-2015

519 views

Category:

Technology


3 download

DESCRIPTION

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

TRANSCRIPT

Page 2: Techfest 2013 No RESTKit for the Weary

Some Amazingly Cool Data from the “Cloud”

the “Cloud”RESTFul* Web

Services

* By “RESTFul,“ of course, I mean REST-ish and JSONy.

Tuesday, January 28, 14

Page 3: Techfest 2013 No RESTKit for the Weary

- (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

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];[[NSURLConnection alloc] initWithRequest:request delegate:self];

Web Service Calls on iOS

NSString *url = @”http://somewebservice.com/objects.json”;

Define your URL

Make the Call

Then implement all of these delegate methods!!!

and now you have to parse the JSON in NSData into something useful...

Tuesday, January 28, 14

Page 4: Techfest 2013 No RESTKit for the Weary

-(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"]; } . . .

NSJSONSerialization is great, but...

Tuesday, January 28, 14

Page 5: Techfest 2013 No RESTKit for the Weary

Enter RestKithttps://github.com/RestKit/RestKit/

RestKit is a framework for consuming and modeling

RESTful web resources on iOS and OS X

Tuesday, January 28, 14

Page 6: Techfest 2013 No RESTKit for the Weary

[[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

Page 7: Techfest 2013 No RESTKit for the Weary

* Open Source & available on GitHub

* Built on AFNetworking

* Beautifully multi-threaded

* Integrates seamlessly with CoreData

* Also works with plain objects

* Handles relationships, nesting, etc.

* Based on mapping like an ORM

* Very configurable

* Handles all REST verbs - GET, POST, PATCH, etc.

* Very actively maintained

* A bunch of other stuff - database seeding, search,

XML, etc.

RestKit is...

Tuesday, January 28, 14

Page 9: Techfest 2013 No RESTKit for the Weary

Using RestKit: The Web Service

Tuesday, January 28, 14

Page 10: Techfest 2013 No RESTKit for the Weary

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

Page 11: Techfest 2013 No RESTKit for the Weary

Using RestKit: The Setup

-(void) setupRestKit { RKObjectManager *objectManager = [RKObjectManager managerWithBaseURL:[NSURL URLWithString:@"http://api.icndb.com/"]]; objectManager.requestSerializationMIMEType=RKMIMETypeJSON; [RKObjectManager setSharedManager:objectManager];

.

.

.

(There are a few extra steps for CoreData, authentication, etc.)

Tuesday, January 28, 14

Page 12: Techfest 2013 No RESTKit for the Weary

Using RestKit: The Mapping

RKObjectMapping *jokeMapping = [RKObjectMapping mappingForClass:[Joke class]];

[jokeMapping addAttributeMappingsFromDictionary:@{ @"id": @"jokeId", @"joke": @"text"}];

RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:jokeMapping method:RKRequestMethodGET pathPattern:@"jokes"

keyPath:@"value" statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];

[[RKObjectManager sharedManager] addResponseDescriptor:responseDescriptor];

Tuesday, January 28, 14

Page 13: Techfest 2013 No RESTKit for the Weary

I <3 Singletons

RKObjectManager is a singleton, so

the setup and mapping steps need

only be done once, usually at app

launch.

Set it and forget it. :)

Tuesday, January 28, 14

Page 14: Techfest 2013 No RESTKit for the Weary

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

Page 15: Techfest 2013 No RESTKit for the Weary

Using RestKit: The Mapping for 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

Page 16: Techfest 2013 No RESTKit for the Weary

Using RestKit: POSTing

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

Page 17: Techfest 2013 No RESTKit for the Weary

Some Demos

Tuesday, January 28, 14