I´m trying to wait for the response using Restkit with Blocks.
Example:
NSArray *myArray = ["RESULT OF REST-REQUEST"];
// Working with the array here.
One of my Block-requests:
- (UIImage*)getPhotoWithID:(NSString*)photoID style:(NSString*)style authToken:(NSString*)authToken {
__block UIImage *image;
NSDictionary *parameter = [NSDictionary dictionaryWithKeysAndObjects:@"auth_token", authToken, nil];
RKURL *url = [RKURL URLWithBaseURLString:@"urlBase" resourcePath:@"resourcePath" queryParameters:parameter];
NSLog(@"%@", [url absoluteString]);
[[RKClient sharedClient] get:[url absoluteString] usingBlock:^(RKRequest *request) {
request.onDidLoadResponse = ^(RKResponse *response) {
NSLog(@"Response: %@", [response bodyAsString]);
image = [UIImage imageWithData:[response body]];
};
}];
return image;
}
You can't return anything in this method since the getting of the image will be asynchronous - it must be -(void)
.
So, what do you do? You should put the action calling this method inside the response block. Be wary of retain cycles in the block.
__block MyObject *selfRef = self;
[[RKClient sharedClient] get:[url absoluteString] usingBlock:^(RKRequest *request) {
request.onDidLoadResponse = ^(RKResponse *response) {
NSLog(@"Response: %@", [response bodyAsString]);
image = [UIImage imageWithData:[response body]];
[selfRef doSomethingWithImage:image];
};
}];