restkit - from zero to hero

Click here to load reader

Post on 15-May-2015

1.659 views

Category:

Technology

2 download

Embed Size (px)

DESCRIPTION

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

TRANSCRIPT

  • 1. Peter Friese, Zhlke EngineeringRestKitFrom Zero to Hero

2. Peter Friese @peterfriese [email protected] xing.to/peter http://peterfriese.de 3. What we will cover today 1 Challenges 2 How can RestKit help? 3 Demos! 4. Challenges...ky Con nectivity Fla 5. Challenges... Differe nt Data Formatsky Con nectivity Fla 6. Challenges...Different Data F ormats ky Con nectivityFlaO fine Data Access 7. How can RestKit help?2 8. RestKit FeaturesIntegrated HTTP StackPluggable ParserObject MappingCore Data Integration 9. Integrated HTTP Stack// create clientRKClient *client =[RKClient clientWithBaseURL:@"http://github.org"];// send this field with each request[clientsetValue:[[UIDevice currentDevice] uniqueIdentifier]forHTTPHeaderField:@"UDID"]; 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. Object Mapping XML (legacy) JSON (new and cool) 12. Object Mapping -># 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. Object Mappingappingr egister mX MLfor a class y) (l egacRKObjectMapping *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. Object Mapping X MLy)(l egac [objectMapping mapKeyPath:@"user.id" toAttribute:@"id"]; @interface GithubUserPeter Friese @property id;36 @property name;[email protected]@property location;42 @property followers;[email protected] email;232107 @property following;@end(simplified code illustration, please bear with me!) 16. Loading Objectscan easilyswitch betweeX MLn (l egac y) dev / prodRKObjectManager *objectManager =[RKObjectManager objectManagerWithBaseURL:@"https://github.com"];[objectManager loadObjectsAtResourcePath:[NSString stringWithFormat:@"api/v2/xml/user/show/%@", userName]objectMapping:objectMappingdelegate:self];- (void)objectLoader:(RKObjectLoader *)objectLoader didLoadObjects:(NSArray *)objects{GithubUser *user = [objects objectAtIndex:0];} 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.mappingdelegate:self];- (void)objectLoader:(RKObjectLoader *)objectLoader didLoadObjects:(NSArray *)objects{GithubUser *user = [objects objectAtIndex:0];} 18. Pluggable ParserJS ON ol) X MLo 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.mappingdelegate:self];- (void)objectLoader:(RKObjectLoader *)objectLoader didLoadObjects:(NSArray *)objects{GithubUser *user = [objects objectAtIndex:0];}:-) 19. Object Mappingappingr egister m ON for a classJSRKObjectMapping *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. Object MappingJS ON [objectMapping mapKeyPath:@"user.id" toAttribute:@"id"];{ @interface GithubUseruser: { @property id;followers_count: 42, @property name;location: "Hamburg", @property location;following_count: 36, @property followers;email: [email protected], @property email;blog: "http://www.peterfriese.de", @property following;name: "Peter Friese", @endid: 232107,}}(simplified code illustration, please bear with me!) 21. Object Mapping nsh ips ela tioR[issueMapping mapKeyPath:@"user"toRelationship:@"user"withMapping:userMapping]; 22. POSTing Objects[[objectManager mappingProvider]setObjectMapping:issueMapping forKeyPath:@""];RKObjectMapping *issueSerializationMapping =[issueMapping inverseMapping];[[objectManager mappingProvider]setSerializationMapping:issueSerializationMappingforClass:[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. 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. Core Data Integration Ofine Data Access Remember? 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. Change Mapped Objects Header Header @interface [email protected] 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. Add a Core Data Model 28. Add a Core Data Model @interface GithubUser : NSObjectKeep in mind: Assign respect iveclasses to ma naged objects! 29. Register a Managed Object Store// set up object managerRKObjectManager *objectManager =[RKObjectManagerobjectManagerWithBaseURL:@"https://api.github.com"];// set up backing data storeobjectManager.objectStore =[RKManagedObjectStoreobjectStoreWithStoreFilename:@"github.sqlite"]; 30. Adjust Object MappingsRKObjectMapping *userMapping =[RKObjectMapping mappingForClass:[GithubUser class]];RKManagedObjectMapping *userMapping =[RKManagedObjectMapping mappingForClass:[GithubUser class]]; 31. Adjust Object Creation GithubIssue *issue = [[GithubIssue alloc] init]; GithubIssue *issue = [GithubIssue object]; 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. 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:resourcePathobjectMapping:mappingdelegate:self];} 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. Putting it All Together 36. Thanks! 37. Thanks! Peter Friese Principal Consultant Zhlke Engineering GmbH Am Sandtorkai 66Available for cons 20457 Hamburg ulting, discussing all thin +49 151 108 604 72gs mobi and frosty beverages Di, 17:30 - 18:30 Mobile CouchDB Peter Friese Mi, 10:15 - 11:15 Auf Tuchfhlung mit smarten Gerten Masanori Fujita