I have a method that uses AFHTTPRequestOperationManager
POST
to retrieve json face features:
+(Face *)getFace:(UIImage *) image
{
__block Face *face = [[Face alloc]init];
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSDictionary *parameters = @{@"selector": @"FULL"};
NSData *imageData = UIImagePNGRepresentation(image);
[manager POST:@"http://api.animetrics.com/v1/detect" parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
[formData appendPartWithFileData: imageData name:@"image" fileName:[NSString stringWithFormat:@"file%d.png",1] mimeType:@"image/png"];
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSError* error;
face = [[Face alloc] initWithString:responseObject error:&error];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
return face;
}
I called this method in my view controller using GCD:
dispatch_queue_t queue = dispatch_queue_create("My Queue",NULL);
dispatch_async(queue, ^{
Face *face = [Face getFace:[UIImage imageNamed:@"image.png"]];
dispatch_async(dispatch_get_main_queue(), ^{
[face getPoints];
});
});
The problem is [face getPoints]
always returns null
because it gets executed before getFace
method finishes retrieving json object. I think that's because AFNetworking itself is using GCD. But how do I fix that? What am I missing?
I am using the latest AFNetworking 2.0.