Frage

Ich benutze AFNetworking -Registrierung neuer Benutzer. Es funktioniert alles in Ordnung, aber im folgenden Block habe ich einige Probleme:

    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;
        }
    }
};

Grundsätzlich habe ich mein Server -Seiten -Skript validiert und feuert einen HTTP -Statuscode zurück (ich weiß, dass 421 nicht gültig ist). Dies ermöglicht es mir zu wissen, was auf dem Server schief gelaufen ist. Dies funktioniert gut.

Mein Problem ist, dass die Antwort, wenn sie zurückkommt NSLog(@"success"); oder NSLog(@"Username taken."); Sofort, aber alle anderen Codes feuert einige Sekunden später.

Kann jemand bitte etwas Licht geben?

War es hilfreich?

Lösung

Hier ist die Lösung für mein Problem, das ist viel besser und viel schneller:

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);

}
 ];

Ich hoffe, das hilft den Menschen.

Andere Tipps

Meine Lösung für den HTTP -Beitrag war dies

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) {
            }];

Damit in diesem Fall eine einzelne Operation auf dem Operation Manager enthält.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top