Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Ways to build REST mobile client

Ways to build REST mobile client

Oursky Limited

June 27, 2013
Tweet

More Decks by Oursky Limited

Other Decks in Programming

Transcript

  1. NetworkManager NetworkConnection NewsBuilder NewsViewController<NetworkManagerDelegate> Rugby Implementation 1. ask for News

    6. News 2. send request 3. response 4. parse response 5. News Thursday, 27 June, 13
  2. RestKit Features Integrated HTTP Stack Pluggable Parser Object Mapping Core

    Data Integration UI Integration Thursday, 27 June, 13
  3. Integrated HTTP Stack Base URLs Custom Header Network Indicator Performing

    Requests Background Processing Authentication Request caching Thursday, 27 June, 13
  4. Integrated HTTP Stack / / create client RKClient *client =

    [RKClient clientWithBaseURL:@”http:/ /github.org”]; / / set header [client setValue:[[[UIDevice currentDevice] identifierForVendor] UUIDString] forHTTPHeaderField:@”X-UDID”]; / / set indicator behavior client.requestQueue.showsNetworkActivityIndicatorWhenBusy = YES; Thursday, 27 June, 13
  5. Performing Request - (IBAction)forkYouWithBlocks { self.imageView.image = nil; [[RKClient sharedClient]

    get:@"http:/ /github.com/fluidicon.png" usingBlock:^(RKRequest *request) { [request setBackgroundPolicy:RKRequestBackgroundPolicyContinue]; [request setOnDidLoadResponse:^(RKResponse *response) { if (response.isSuccessful) { UIImage *image = [UIImage imageWithData:response.body]; self.imageView.image = image; } }]; }]; } Thursday, 27 June, 13
  6. Background Processing [request setBackgroundPolicy:RKRequestBackgroundPolicyContinue]; RKRequestBackgroundPolicyNone / / do nothing RKRequestBackgroundPolicyCancel

    / / cancel request RKRequestBackgroundPolicyContinue / / continue RKRequestBackgroundPolicyRequeue / / requeue upon app restart Thursday, 27 June, 13
  7. Object mapping { id: 1, name: “Peter”, location: “Hong Kong”,

    followers: 1, email: “[email protected]”, following: 36 } @interface User : NSObject @property (strong, nonatomic) NSNumber *id; @property (strong, nonatomic) NSString *name; @property (strong, nonatomic) NSString *location; @property (strong, nonatomic) NSNumber *followers; @property (strong, nonatomic) NSString *email; @property (strong, nonatomic) NSNumber *following; @end Thursday, 27 June, 13
  8. Object mapping / / register mapping for a class objectMapping

    = [RKObjectMapping mappingForClass:[User class]]; [objectMapping mapKeyPath:@"id" toAttribute:@"id"]; [objectMapping mapKeyPath:@"name" toAttribute:@"name"]; [objectMapping mapKeyPath:@"location" toAttribute:@"location"]; [objectMapping mapKeyPath:@"email" toAttribute:@"email"]; [objectMapping mapKeyPath:@"following" toAttribute:@"following"]; [objectMapping mapKeyPath:@"followers" toAttribute:@"followers"]; Thursday, 27 June, 13
  9. Object mapping [[RKObjectManager sharedManager] loadObjectsAtResourcePath:[NSString stringWithFormat:@"/users/%@", userName] usingBlock:^(RKObjectLoader *loader) {

    [SVProgressHUD showWithStatus:@"Loading..."]; [loader setObjectMapping:objectMapping]; [loader setOnDidLoadObject:^(id object) { [SVProgressHUD dismiss]; }]; [loader setOnDidFailWithError:^(NSError *error) { [SVProgressHUD showErrorWithStatus:@"Problem loading user"]; }]; }]; Thursday, 27 June, 13
  10. Mapping Relationship RKObjectMapping *userMapping = [RKObjectMapping mappingForClass:[User class]]; RKObjectMapping *issueMapping

    = [RKObjectMapping mappingForClass:[Issue class]]; / / create relationships [issueMapping mapKeyPath:@"user" toRelationship:@"user" withMapping:userMapping]; { number: 1, title: “Issue #1”, user: { ... } } @interface Issue : NSObject @property (strong, nonatomic) NSNumber *number; @property (strong, nonatomic) NSString *title; @property (strong, nonatomic) User *user; @end Thursday, 27 June, 13
  11. Why Volley Restful apps have common features: infinite scroll image

    loading offline caching request priority Volley provides all of them Thursday, 27 June, 13