Last active
December 18, 2015 13:39
-
-
Save bragisig/5791802 to your computer and use it in GitHub Desktop.
Call a GET method with RestKit v0.20 and rmap the results
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
RKLogConfigureByName("RestKit/Network", RKLogLevelTrace); | |
RKLogConfigureByName("RestKit/ObjectMapping", RKLogLevelTrace); | |
//NSString *url = @"http://0.0.0.0:5001/"; | |
NSString *url = @"https://asteroids-nemesis.herokuapp.com/"; | |
NSURL *baseURL = [NSURL URLWithString:url]; | |
AFHTTPClient * client = [AFHTTPClient clientWithBaseURL:baseURL]; | |
[client setDefaultHeader:@"Accept" value:RKMIMETypeJSON]; | |
_restKitObjectManager = [[RKObjectManager alloc] initWithHTTPClient:client]; | |
[_restKitObjectManager.HTTPClient setDefaultHeader:@"some-custom-header" value:@"ABC"]; | |
[_restKitObjectManager setRequestSerializationMIMEType:RKMIMETypeJSON]; | |
RKObjectMapping *gamerMapping = [RKObjectMapping mappingForClass:[Player class]]; | |
[gamerMapping addAttributeMappingsFromDictionary:@{ | |
@"username" : @"username", | |
@"score" : @"score" | |
}]; | |
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:gamerMapping pathPattern:nil keyPath:@"leaderboard" statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)]; | |
[_restKitObjectManager addResponseDescriptor:responseDescriptor]; | |
NSString *path = [NSString stringWithFormat:@"highscores/%i/%i", fromRank, toRank]; | |
//Call the webservice | |
[_restKitObjectManager getObjectsAtPath:path parameters:nil | |
success:^(RKObjectRequestOperation * operation, RKMappingResult *mappingResult) | |
{ | |
//NSLog(@"success: mappings: %@", mappingResult); | |
NSArray *result = [mappingResult array]; | |
//Call the delegate with the results | |
[_delegate loadHighscoresResult:result]; | |
} | |
failure:^(RKObjectRequestOperation * operaton, NSError * error) | |
{ | |
NSLog (@"failure: operation: %@ \n\nerror: %@", operaton, error); | |
}]; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment