Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AFNetworking, AFHTTPRequestOperation completion block slow to fire code

I am using AFNetworking registering new users, it all works ok but on the following block I have some issues:

    AFHTTPRequestOperation *operation = [[[AFHTTPRequestOperation alloc] initWithRequest:myRequest] autorelease];
operation.completionBlock = ^ {
    if ([operation hasAcceptableStatusCode]) {
        NSLog(@"success");
        username.backgroundColor = [UIColor yellowColor];
    } else {
        switch ([operation.response statusCode]) {
            case 421:                  
            {
                NSLog(@"Username taken.");
                username.backgroundColor = [UIColor yellowColor];   
            }
                break;
            default:
                break;
        }
    }
};

Basically I my server side script does some validation and fires back a HTTP status code (I know 421 isn't a valid one). This enables me to know what went wrong on the server, this works well.

My issue is that when the response comes back it fires the NSLog(@"success"); or NSLog(@"Username taken."); straight away but any other codes fires of quite a few seconds later.

Can anyone shed any light on this please?

like image 245
iamsmug Avatar asked Nov 29 '11 21:11

iamsmug


1 Answers

Here is the solution to my problem, this is much better and a hell of a lot faster:

AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc]initWithRequest:request];
[operation  setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {

    NSLog(@"success: %@", operation.responseString);

    [SVProgressHUD dismissWithSuccess:@"Sucess!" afterDelay:2];
    [self saveContinue:operation.responseString];


} 
    failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"error: %@",  operation.responseString);

}
 ];

I hope this help people.

like image 159
iamsmug Avatar answered Nov 06 '22 16:11

iamsmug