Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RestKit 0.2, multiple ways to make a GET,POST,PUT request

I am getting slightly confused as to the way to use restkit, it seems like there is multiple ways to do the same thing, Before i was content just messing about with it till it worked, but now he has changed the framework and usage in 0.20.x, And I have spent a great deal of time converting my code over and now spending even more time trying to get it to work like before. And I have scoured for some examples and such and the ones that people claim to work for them, do not do so much for me, so there must be a difference somewhere, so perhaps someone can tell me the difference in the following when attempting to get this data at least for example 1 & 2 (which fails but that is for a question on the restkit github)

response.body={
"player": [
 {
  "_id": "50585c86ded998e77a000002"
 }
]
}

1.

[[RKObjectManager sharedManager].router.routeSet addRoute:[RKRoute routeWithClass:[Player class] pathPattern:@"player/fbid/:fID" method:RKRequestMethodGET]];

And to get your player, something like this:

Player *player = [Player new];
player.playerID = 2;
[[RKObjectManager sharedManager] getObject:player path:nil parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *result) 
{
// Request 
} failure:nil];

2.

Using a response Descriptor like so.

RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor  responseDescriptorWithMapping:playerWtfMappingIn pathPattern:nil keyPath:@"player" statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];

[[RKObjectManager sharedManager] addResponseDescriptor:responseDescriptor];

[tempPlayer setFbID:[result objectForKey:@"id"]];

[objectManager getObject:tempPlayer path:nil parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult)
{

}
failure:nil];

3.

Or in fact blakes own exmaple on the RestKit wiki

RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:articleMapping pathPattern:nil keyPath:@"articles" statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];

NSURL URL = [NSURL URLWithString:@"http://restkit.org/articles"];
NSURLRequest *request = [NSURLRequest requestWithURL:URL];
RKObjectRequestOperation *objectRequestOperation = [[RKObjectRequestOperation alloc] initWithRequest:request responseDescriptors:@[ responseDescriptor ]];
[objectRequestOperation setCompletionBlockWithSuccess:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
    RKLogInfo(@"Load collection of Articles: %@", objects);
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
    RKLogError(@"Operation failed with error: %@", error);
}];

[objectRequestOperation start];

And I am sure there are many other ways, My suspicion if anyone can confirm it is that the way you should setup your path's and whether you use one method over another is decided quite strongly by the data set you are trying to map and it's format. Then again some of it also looks like a different way of doing the exact same thing? Thanks

like image 345
Genhain Avatar asked Dec 18 '12 06:12

Genhain


1 Answers

both way are correct. In fact, the first one is even better, since it centralize all your configuration in the RKObjectManager. For a project which is based on RestKit, I highly suggest you use the ObjectManager way :)

You can even go a step further and start using routes to keep your code cleaner, see the wiki about that part :)

Hope it helps

like image 189
allaire Avatar answered Oct 30 '22 08:10

allaire