Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

My solution for HTTP POST was this

NSData *data = [self.postBody dataUsingEncoding:NSUTF8StringEncoding];
            NSURL *url = [NSURL URLWithString:self.requestUrl];
            NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
            [request setURL:url];
            [request addValue:@"application/octet-stream" forHTTPHeaderField: @"Content-Type"];
            [request addValue:[NSString stringWithFormat:@"%lu", (unsigned long)[data length]] forHTTPHeaderField:@"Content-Length"];
            [request setHTTPMethod:@"POST"];
            NSMutableData *requestBody = [NSMutableData data];
            [requestBody appendData:data];
            [request setHTTPBody:requestBody];
            AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
            operation.responseSerializer = [AFJSONResponseSerializer serializer];
            operation.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/plain"];
            [operation  setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
                NSInteger statusCode = operation.response.statusCode;
                [self requestFinished:responseObject andStatusCode:statusCode];
            } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                [self requestFailed:error];
            }];
            [[self.requestManager operationQueue] addOperation:operation];

            [AFHTTPRequestOperation batchOfRequestOperations:[NSArray arrayWithObjects:operation, nil] progressBlock:^(NSUInteger numberOfFinishedOperations, NSUInteger totalNumberOfOperations) {
            } completionBlock:^(NSArray *operations) {
            }];

that enqueues a single operation on the operation manager in this case.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top