Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
by Peter Markshttp://petermarks.info
Restkit is a RESTful Object Mapping  Framework for iOS and OSX.
Server - Client
Server - ClientWeb Application
Server - ClientWeb Application   Database
Server - Client  Web Application     DatabaseRESTful JSON or XML     webservice
Server - Client  Web Application                       Obj C Application     DatabaseRESTful JSON or XML     webservice
Server - Client  Web Application                       Obj C Application     Database                             Object g...
Server - Client  Web Application                       Obj C Application     Database                             Object g...
Server - Client  Web Application                       Obj C Application     Database                             Object g...
Server - Client                        GET Request  Web Application                       Obj C Application     Database  ...
Server - Client                        GET Request  Web Application                       Obj C Application     Database  ...
Server - Client                        GET Request  Web Application                       Obj C Application     Database  ...
Two representations of the same data:
Two representations of the same data:/contacts
Two representations of the same data:/contacts[{



contact:
{







id:
1234,







full_name:
Peter
Marks,







emai...
Two representations of the same data:/contacts                                   @interface Contact : RKObject {          ...
Two representations of the same data:/contacts                                   @interface Contact : RKObject {          ...
Fetch objects from server- (void)applicationDidFinishLaunching:(UIApplication*)application withOptions:(NSDictionary*)opti...
Set up routes- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions ...
Post object to server- (void) postObject {   Contact * newContact = [[Contact alloc] init];   newContact.fullName = @"Pete...
Core Data
Core Data@interface Contact : RKManagedObject {}@end@implementation Contact@synthesize ...;+ (NSDictionary*)elementToPrope...
Relationship modeling[{"project":
{



"id":
123,



"name":
"Produce
RestKit
Sample
Code",



"description":
"We
need
mor...
Relationship mapping
Relationship mapping@interface Project : RKManagedObject {  NSNumber* _projectID;  NSString* _name;  NSString* _descriptio...
Relationship mapping@interface Project : RKManagedObject {  NSNumber* _projectID;                                      @im...
Relationship serialization+ (NSArray*)relationshipsToSerialize {  return [NSArray arrayWithObject:@"tasks"];}[{"project":
...
Object mapping 2.0
Object mapping 2.0Everything mentioned is for the current version of 0.9.2
Object mapping 2.0Everything mentioned is for the current version of 0.9.2Upcoming version 0.9.3 overhauls of Object mapping
Object mapping 2.0Everything mentioned is for the current version of 0.9.2Upcoming version 0.9.3 overhauls of Object mappi...
Object mapping 2.0          Everything mentioned is for the current version of 0.9.2          Upcoming version 0.9.3 overh...
Other cool stuff
RKRequestQueue
RKRequestQueue- (void) postObject {   Contact * newContact = [[Contact alloc] init];   newContact.fullName = @"Peter Marks...
RKRequestQueue- (void) postObject {   Contact * newContact = [[Contact alloc] init];   newContact.fullName = @"Peter Marks...
Background requests- (void)backgroundUpload {   RKRequest* request = [[RKClient sharedClient] post:@"somewhere" delegate:s...
Multi-part requests// Create an AttachmentRKParamsAttachment* attachment = [params setFile:myFilePath forParam:@"image1"];...
Database seedingRKManagedObjectSeeder* seeder = [RKManagedObjectSeeder objectSeederWithObjectManager:                     ...
Integration LayersRuby on RailsRKRailsRouter – A Router implementation aware of Ruby on Rails idiomsThree20RKRequestTTMode...
More information• http://Restkit.org• http://github.com/twotoasters/RestKit/• http://groups.google.com/group/restkit
Canned discussion prompt:How do others solve this problem?
Upcoming SlideShare
Loading in …5
×

Introduction to Restkit

16,730 views

Published on

Given at PDX Cocoaheads on July 27, 2011

Published in: Technology, News & Politics
  • I don't have RKDynamicRouter...
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • hi, if copy the slide 18, 19, 20 and 21 in my button 'Send' my User is create? really?! I haven't change something?
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here

Introduction to Restkit

  1. 1. by Peter Markshttp://petermarks.info
  2. 2. Restkit is a RESTful Object Mapping Framework for iOS and OSX.
  3. 3. Server - Client
  4. 4. Server - ClientWeb Application
  5. 5. Server - ClientWeb Application Database
  6. 6. Server - Client Web Application DatabaseRESTful JSON or XML webservice
  7. 7. Server - Client Web Application Obj C Application DatabaseRESTful JSON or XML webservice
  8. 8. Server - Client Web Application Obj C Application Database Object graphRESTful JSON or XML webservice
  9. 9. Server - Client Web Application Obj C Application Database Object graphRESTful JSON or XML Restkit webservice
  10. 10. Server - Client Web Application Obj C Application Database Object graphRESTful JSON or XML Restkit webservice
  11. 11. Server - Client GET Request Web Application Obj C Application Database Object graphRESTful JSON or XML Restkit webservice
  12. 12. Server - Client GET Request Web Application Obj C Application Database Object graphRESTful JSON or XML Restkit webservice
  13. 13. Server - Client GET Request Web Application Obj C Application Database Object graph POST RequestRESTful JSON or XML Restkit webservice
  14. 14. Two representations of the same data:
  15. 15. Two representations of the same data:/contacts
  16. 16. Two representations of the same data:/contacts[{



contact:
{







id:
1234,







full_name:
Peter
Marks,







email:
petertmarks@gmail.com,



}},{



contact:
{







id:
3456,







full_name:
Barack
Obama,







email:
barack94@aol.com,



}}]
  17. 17. Two representations of the same data:/contacts @interface Contact : RKObject { NSNumber* _contactID; NSString* _fullName; NSString* _email;[{ }



contact:
{







id:
1234, @property (retain) NSNumber* contactID; @property (retain) NSString* fullName;







full_name:
Peter
Marks, @property (retain) NSString* email;







email:
petertmarks@gmail.com, @end



}},{



contact:
{







id:
3456,







full_name:
Barack
Obama,







email:
barack94@aol.com,



}}]
  18. 18. Two representations of the same data:/contacts @interface Contact : RKObject { NSNumber* _contactID; NSString* _fullName; NSString* _email;[{ }



contact:
{







id:
1234, @property (retain) NSNumber* contactID; @property (retain) NSString* fullName;







full_name:
Peter
Marks, @property (retain) NSString* email;







email:
petertmarks@gmail.com, @end



}}, @implementation Contact @synthesize ...;{



contact:
{ + (NSDictionary*)elementToPropertyMappings {







id:
3456, return [NSDictionary dictionaryWithKeysAndObjects: @"id", @"contactID",







full_name:
Barack
Obama, @"full_name", @"fullName",







email:
barack94@aol.com, @"email", @"email",



} nil];}] } @end
  19. 19. Fetch objects from server- (void)applicationDidFinishLaunching:(UIApplication*)application withOptions:(NSDictionary*)options { RKClient* client = [RKClient clientWithBaseURL:@"http://restkit.org"];}- (void)loadContact { RKObjectManager* manager = [RKObjectManager objectManagerWithBaseURL:@"http://restkit.org"]; [manager loadObjectsAtResourcePath:@"/contacts/1" objectClass:[Contact class] delegate:self]}- (void)objectLoader:(RKObjectLoader*)objectLoader didLoadObjects:(NSArray*)objects { Contact* contact = [objects objectAtIndex:0]; NSLog(@"Loaded Contact ID #%@ -> Name: %@, Company: %@", contact.id, contact.name, contact.company);}
  20. 20. Set up routes- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { RKDynamicRouter* router = [RKDynamicRouter new]; // Define a default resource path for all unspecified HTTP verbs [router routeClass:[Contact class] toResourcePath:@"/contacts/(contactID)"]; [router routeClass:[Contact class] toResourcePath:@"/contacts" forMethod:RKRequestMethodPOST]; [RKObjectManager sharedManager].router = router;}
  21. 21. Post object to server- (void) postObject { Contact * newContact = [[Contact alloc] init]; newContact.fullName = @"Peter Marks"; newContact.email = @"petertmarks@gmail.com"; [[RKObjectManager sharedManager] postObject:newContact delegate:self];}- (void)objectLoader:(RKObjectLoader*)objectLoader didLoadObjects:(NSArray*)objects { Contact* contact = [objects objectAtIndex:0]; NSLog(@"Loaded Contact ID #%@ -> Name: %@, Company: %@", contact.contactID, contact.fullName, contact.company);}
  22. 22. Core Data
  23. 23. Core Data@interface Contact : RKManagedObject {}@end@implementation Contact@synthesize ...;+ (NSDictionary*)elementToPropertyMappings { return [NSDictionary dictionaryWithKeysAndObjects: @"id", @"contactID", @"full_name", @"fullName", @"email", @"email", nil];}+ (NSString*)primaryKeyProperty { return @"contactID";}@end
  24. 24. Relationship modeling[{"project":
{



"id":
123,



"name":
"Produce
RestKit
Sample
Code",



"description":
"We
need
more
sample
code!",



"user":
{







"id":
1,







"name":
"Blake
Watters",







"email":
"blake@twotoasters.com"



},



"tasks":
[







{"id":
1,
"name":
"Identify
samples
to
write",
"assigned_user_id":
1},







{"id":
2,
"name":
"Write
the
code",
"assigned_user_id":
1},







{"id":
3,
"name":
"Push
to
Github",
"assigned_user_id":
1},







{"id":
4,
"name":
"Update
the
mailing
list",
"assigned_user_id":
1}



]}},
  25. 25. Relationship mapping
  26. 26. Relationship mapping@interface Project : RKManagedObject { NSNumber* _projectID; NSString* _name; NSString* _description; NSString* _userID; User* _user; NSArray* _tasks;}@property (nonatomic, retain) NSNumber* projectID;@property (nonatomic, retain) NSString* name;@property (nonatomic, retain) NSString* description;@property (nonatomic, retain) NSString* userID;@property (nonatomic, retain) User* user;@property (nonatomic, retain) NSArray* tasks;@end
  27. 27. Relationship mapping@interface Project : RKManagedObject { NSNumber* _projectID; @implementation Project NSString* _name; @dynamic ...; NSString* _description; NSString* _userID; + (NSDictionary*)elementToPropertyMappings {...} User* _user; NSArray* _tasks; - (NSString*) primaryKeyProperty {} return projectID; }@property (nonatomic, retain) NSNumber* projectID;@property (nonatomic, retain) NSString* name; + (NSDictionary*)elementToRelationshipMappings {@property (nonatomic, retain) NSString* description; return [NSDictionary dictionaryWithKeysAndObjects:@property (nonatomic, retain) NSString* userID; @"user", @"user",@property (nonatomic, retain) User* user; @"tasks", @"tasks",@property (nonatomic, retain) NSArray* tasks; nil]; }@end + (NSDictionary*)relationshipToPrimaryKeyPropertyMappings { return [NSDictionary dictionaryWithObject:@"userID" forKey:@"user"]; } @end
  28. 28. Relationship serialization+ (NSArray*)relationshipsToSerialize { return [NSArray arrayWithObject:@"tasks"];}[{"project":
{



"id":
123,



"name":
"Produce
RestKit
Sample
Code",



"description":
"We
need
more
sample
code!",



"tasks":
[







{"id":
1,
"name":
"Identify
samples
to
write",
"assigned_user_id":
1},







{"id":
2,
"name":
"Write
the
code",
"assigned_user_id":
1},







{"id":
3,
"name":
"Push
to
Github",
"assigned_user_id":
1},







{"id":
4,
"name":
"Update
the
mailing
list",
"assigned_user_id":
1}



]}},
  29. 29. Object mapping 2.0
  30. 30. Object mapping 2.0Everything mentioned is for the current version of 0.9.2
  31. 31. Object mapping 2.0Everything mentioned is for the current version of 0.9.2Upcoming version 0.9.3 overhauls of Object mapping
  32. 32. Object mapping 2.0Everything mentioned is for the current version of 0.9.2Upcoming version 0.9.3 overhauls of Object mappingMain difference is that mappings are no longer dependenton class inheritance.
  33. 33. Object mapping 2.0 Everything mentioned is for the current version of 0.9.2 Upcoming version 0.9.3 overhauls of Object mapping Main difference is that mappings are no longer dependent on class inheritance.// In this use-case Article is a vanilla NSObject with propertiesRKObjectMapping* mapping = [RKObjectMapping mappingForClass:[Article class]];// Add an attribute mapping to the object mapping directlyRKObjectAttributeMapping* titleMapping = [RKObjectAttributeMapping mappingFromKeyPath:@"title" toKeyPath:@"title"];[mapping addAttributeMapping:titleMapping];
  34. 34. Other cool stuff
  35. 35. RKRequestQueue
  36. 36. RKRequestQueue- (void) postObject { Contact * newContact = [[Contact alloc] init]; newContact.fullName = @"Peter Marks"; newContact.email = @"petertmarks@gmail.com"; [[RKObjectManager sharedManager] postObject:newContact delegate:self];}- (void)objectLoader:(RKObjectLoader*)objectLoader didLoadObjects:(NSArray*)objects { Contact* contact = [objects objectAtIndex:0]; NSLog(@"Loaded Contact ID #%@ -> Name: %@, Company: %@",contact.contactID, contact.fullName, contact.company);}
  37. 37. RKRequestQueue- (void) postObject { Contact * newContact = [[Contact alloc] init]; newContact.fullName = @"Peter Marks"; newContact.email = @"petertmarks@gmail.com"; [[RKObjectManager sharedManager] postObject:newContact delegate:self];}- (void)objectLoader:(RKObjectLoader*)objectLoader didLoadObjects:(NSArray*)objects { Contact* contact = [objects objectAtIndex:0]; NSLog(@"Loaded Contact ID #%@ -> Name: %@, Company: %@",contact.contactID, contact.fullName, contact.company);}// We have been obscured -- cancel any pending requests- (void)viewWillDisappear:(BOOL)animated { [[RKRequestQueue sharedQueue] cancelRequestsWithDelegate:self];}
  38. 38. Background requests- (void)backgroundUpload { RKRequest* request = [[RKClient sharedClient] post:@"somewhere" delegate:self]; request.backgroundPolicy = RKRequestBackgroundPolicyNone; // Take no action with regard to backgrounding request.backgroundPolicy = RKRequestBackgroundPolicyCancel; // If the app switches to the background, cancel the request request.backgroundPolicy = RKRequestBackgroundPolicyContinue; // Continue the request in the background request.backgroundPolicy = RKRequestBackgroundPolicyRequeue; // Cancel the request and place it back on the queue for nextactivation}
  39. 39. Multi-part requests// Create an AttachmentRKParamsAttachment* attachment = [params setFile:myFilePath forParam:@"image1"];attachment.MIMEType = @"image/gif";attachment.fileName = @"picture.gif";// Attach an Image from the App BundleUIImage* image = [UIImage imageNamed:@"another_image.png"];NSData* imageData = UIImagePNGRepresentation(image);[params setData:imageData MIMEType:@"image/png" forParam:@"image2"];// Send a Request![[RKClient sharedClient] post:@"/uploadImages" params:params delegate:self];
  40. 40. Database seedingRKManagedObjectSeeder* seeder = [RKManagedObjectSeeder objectSeederWithObjectManager: [RKObjectManager sharedManager]];// Seed the database with User objects. The class will be inferred via element registration[seeder seedObjectsFromFiles:@"users.json", nil];
  41. 41. Integration LayersRuby on RailsRKRailsRouter – A Router implementation aware of Ruby on Rails idiomsThree20RKRequestTTModel – An implementation of the TTModel protocol forThree20 that allows RestKit object loaders to drive Three20 tables
  42. 42. More information• http://Restkit.org• http://github.com/twotoasters/RestKit/• http://groups.google.com/group/restkit
  43. 43. Canned discussion prompt:How do others solve this problem?

×