RestKit - From Zero to Hero

1,855 views
1,756 views

Published on

RestKit - From Zero to Hero
This talk explains what RestKit is and how it can help you build applications that sync with REST services.

Published in: Technology, Education
2 Comments
3 Likes
Statistics
Notes
  • what the toResourcePath? it's my URL?
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • issueMapping what is it? and @'user', it's the twice it's a reference to the path for my Restful services?
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Views
Total views
1,855
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
47
Comments
2
Likes
3
Embeds 0
No embeds

No notes for slide

RestKit - From Zero to Hero

  1. 1. Peter Friese, Zühlke EngineeringRestKitFrom Zero to Hero
  2. 2. Peter Friese @peterfriese peter.friese@zuehlke.com xing.to/peter http://peterfriese.de
  3. 3. What we will cover today 1 Challenges 2 How can RestKit help? 3 Demos!
  4. 4. Challenges... ky Con nectivity Fla
  5. 5. Challenges... Differe nt Data F ormats ky Con nectivity Fla
  6. 6. Challenges... Differe nt Data F ormats ky Con nectivity FlaO ffline Data Access
  7. 7. How can RestKit help? 2
  8. 8. RestKit Features Integrated HTTP Stack Pluggable Parser Object Mapping Core Data Integration
  9. 9. Integrated HTTP Stack // create client RKClient *client = [RKClient clientWithBaseURL:@"http://github.org"]; // send this field with each request [client setValue:[[UIDevice currentDevice] uniqueIdentifier] forHTTPHeaderField:@"UDID"];
  10. 10. Integrated HTTP Stack - (IBAction)forkYou:(id)sender { [[RKClient sharedClient] get:@"https://github.com/fluidicon.png" delegate:self]; } - (void)request:(RKRequest *)request didLoadResponse:(RKResponse *)response { if ([response isSuccessful]) { UIImage *image = [UIImage imageWithData:[response body]]; self.imageView.image = image; } }
  11. 11. Object Mapping XML (legacy) JSON (new and cool)
  12. 12. Object Mapping -> #
  13. 13. Object Mapping @interface GithubUser : NSObject @property (strong, nonatomic) NSNumber *id; @property (strong, nonatomic) NSString *name; -> @property (strong, nonatomic) NSString *location; @property (strong, nonatomic) NSString *followers; @property (strong, nonatomic) NSString *email; @property (strong, nonatomic) NSString *following; @end
  14. 14. Object Mapping apping r egister mX ML for a class y) (l egac RKObjectMapping *objectMapping = [RKObjectMapping mappingForClass:[GithubUser class]]; [objectMapping mapKeyPath:@"user.id" confi toAttribute:@"id"]; gure [objectMapping mapKeyPath:@"user.name" mappi toAttribute:@"name"]; ng (KV [objectMapping mapKeyPath:@"user.location" C) toAttribute:@"location"]; [objectMapping mapKeyPath:@"user.following-count" toAttribute:@"following"]; [objectMapping mapKeyPath:@"user.followers-count" toAttribute:@"followers"]
  15. 15. Object Mapping X ML y) (l egac [objectMapping mapKeyPath:@"user.id" toAttribute:@"id"];<user> @interface GithubUser <name>Peter Friese @property id; <following-count">36 @property name; <email>peter@peterfriese.de @property location; <followers-count">42 @property followers; <location>Hamburg @property email; <id>232107 @property following;</user> @end(simplified code illustration, please bear with me!)
  16. 16. Loading Objects can easily switch betweeX ML n (l egac y) dev / prodRKObjectManager *objectManager = [RKObjectManager objectManagerWithBaseURL:@"https://github.com"];[objectManager loadObjectsAtResourcePath: [NSString stringWithFormat:@"api/v2/xml/user/show/%@", userName] objectMapping:objectMapping delegate:self];- (void)objectLoader:(RKObjectLoader *)objectLoader didLoadObjects:(NSArray *)objects{ GithubUser *user = [objects objectAtIndex:0];}
  17. 17. Pluggable ParserRKObjectManager *objectManager = [RKObjectManager objectManagerWithBaseURL:@"https://github.com"];// we want to send and receive JSONobjectManager.serializationMIMEType = RKMIMETypeJSON;objectManager.acceptMIMEType = RKMIMETypeJSON;[objectManager loadObjectsAtResourcePath: [NSString stringWithFormat:@"api/v2/json/user/show/%@", userName] objectMapping:self.mapping delegate:self];- (void)objectLoader:(RKObjectLoader *)objectLoader didLoadObjects:(NSArray *)objects{ GithubUser *user = [objects objectAtIndex:0];}
  18. 18. Pluggable Parser JS ON ol) X ML o dcRKObjectManagern *objectManager = acy) wa (leg (ne [RKObjectManager objectManagerWithBaseURL:@"https://github.com"];// we want to send and receive JSONobjectManager.serializationMIMEType = RKMIMETypeJSON;objectManager.acceptMIMEType = RKMIMETypeJSON;[objectManager loadObjectsAtResourcePath: [NSString stringWithFormat:@"api/v2/json/user/show/%@", userName] objectMapping:self.mapping delegate:self];- (void)objectLoader:(RKObjectLoader *)objectLoader didLoadObjects:(NSArray *)objects{ GithubUser *user = [objects objectAtIndex:0];} :-)
  19. 19. Object Mapping apping r egister m ON for a classJS RKObjectMapping *objectMapping = [RKObjectMapping mappingForClass:[GithubUser class]]; [objectMapping mapKeyPath:@"user.id" confi toAttribute:@"id"]; gure [objectMapping mapKeyPath:@"user.name" mappi toAttribute:@"name"]; ng (KV [objectMapping mapKeyPath:@"user.location" C) toAttribute:@"location"]; [objectMapping mapKeyPath:@"user.following_count" toAttribute:@"following"]; [objectMapping mapKeyPath:@"user.followers_count" toAttribute:@"followers"]
  20. 20. Object MappingJS ON [objectMapping mapKeyPath:@"user.id" toAttribute:@"id"];{ @interface GithubUser user: { @property id; followers_count: 42, @property name; location: "Hamburg", @property location; following_count: 36, @property followers; email: “peter@peterfriese.de, @property email; blog: "http://www.peterfriese.de", @property following; name: "Peter Friese", @end id: 232107, } }(simplified code illustration, please bear with me!)
  21. 21. Object Mapping nsh ips ela tioR [issueMapping mapKeyPath:@"user" toRelationship:@"user" withMapping:userMapping];
  22. 22. POSTing Objects[[objectManager mappingProvider] setObjectMapping:issueMapping forKeyPath:@""];RKObjectMapping *issueSerializationMapping = [issueMapping inverseMapping];[[objectManager mappingProvider] setSerializationMapping:issueSerializationMapping forClass:[GithubIssue class]];[[objectManager router] routeClass:[GithubIssue class] toResourcePath:@"/repos/:repouser/:repo/issues/:number"];[[objectManager router] routeClass:[GithubIssue class] toResourcePath:@"/repos/:repouser/:repo/issues" forMethod:RKRequestMethodPOST ];
  23. 23. POSTing ObjectsCreate new objectGithubIssue *issue = [[GithubIssue alloc] init];Infos for RestKit router:issue.repouser = repouser;issue.repo = repo;POST object:[[RKObjectManager sharedManager] postObject:issue delegate:self];
  24. 24. Core Data Integration Offline Data Access Remember?
  25. 25. Core Data Integration Change Mapped Objects Add a Core Data Model Register a Managed Object Store Adjust Object Mappings Adjust Object Creation Fetch Data from DB / Backend
  26. 26. Change Mapped Objects Header Header @interface GithubUser @interface GithubUser : NSObject : NSManagedObject Module Module @synthesize id; @dynamic id; @synthesize login; @dynamic login; @synthesize name; @dynamic name; @synthesize company; @dynamic company; @synthesize location; @dynamic location; @synthesize blog; @dynamic blog; @synthesize following; @dynamic following; @synthesize followers; @dynamic followers; @synthesize email; @dynamic email;
  27. 27. Add a Core Data Model
  28. 28. Add a Core Data Model @interface GithubUser : NSObject Keep in mind: Assign respect ive classes to ma naged objects!
  29. 29. Register a Managed Object Store // set up object manager RKObjectManager *objectManager = [RKObjectManager objectManagerWithBaseURL:@"https://api.github.com"]; // set up backing data store objectManager.objectStore = [RKManagedObjectStore objectStoreWithStoreFilename:@"github.sqlite"];
  30. 30. Adjust Object MappingsRKObjectMapping *userMapping = [RKObjectMapping mappingForClass:[GithubUser class]];RKManagedObjectMapping *userMapping = [RKManagedObjectMapping mappingForClass:[GithubUser class]];
  31. 31. Adjust Object Creation GithubIssue *issue = [[GithubIssue alloc] init]; GithubIssue *issue = [GithubIssue object];
  32. 32. Fetch Data from DB / Backend Online of offline? if ([[RKObjectManager sharedManager] isOnline]) { [self fetchDataFromRemote]; } else { [self fetchDataFromDataStore]; } Offline - Fetch from DB - (void)fetchDataFromDataStore { repos = [GithubRepo allObjects]; [self.tableView reloadData]; }
  33. 33. Fetch Data from DB / BackendOnline - Fetch from Backend- (void)fetchDataFromRemote{ RKObjectMapping *mapping = [[[RKObjectManager sharedManager] mappingProvider] objectMappingForClass:[GithubRepo class]]; NSString *resourcePath = [NSString stringWithFormat:@"/users/%@/repos", loginInfo.login]; [[RKObjectManager sharedManager] loadObjectsAtResourcePath:resourcePath objectMapping:mapping delegate:self];}
  34. 34. Fetch Data from DB / BackendReconnect after offline- (void)reachabilityChanged:(NSNotification*)notification { RKReachabilityObserver* observer = (RKReachabilityObserver*)[notification object]; if ([observer isNetworkReachable]) { if (![self.view isHidden]) { [self fetchDataFromRemote]; } } else { if (![self.view isHidden]) { [self fetchDataFromDataStore]; } }}
  35. 35. Putting it All Together
  36. 36. Thanks!
  37. 37. Thanks! Peter Friese Principal Consultant Zühlke Engineering GmbH Am Sandtorkai 66 Available for cons 20457 Hamburg ulting, discussing all thin +49 151 108 604 72 gs mobi and frosty beverag es Di, 17:30 - 18:30 Mobile CouchDB Peter Friese Mi, 10:15 - 11:15 Auf Tuchfühlung mit smarten Geräten Masanori Fujita

×