2011-11-29 15 views
9

Sto usando AFNetworking registrazione di nuovi utenti, tutto funziona bene, ma il seguente blocco ho alcuni problemi:AFNetworking, AFHTTPRequestOperation blocco completamento lento a fuoco codice

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

Fondamentalmente io il mio script lato server fa qualche convalida e restituisce un codice di stato HTTP (so che 421 non è valido). Questo mi consente di sapere cosa è andato storto sul server, questo funziona bene.

Il mio problema è che quando la risposta ritorna, spara immediatamente lo NSLog(@"success"); o NSLog(@"Username taken."); ma altri codici si accendono di qualche secondo dopo.

Qualcuno può far luce su questo per favore?

+0

Ho trovato una soluzione a questo problema ora. – iamsmug

+1

Puoi postarlo come risposta, quindi per favore, così altre persone con lo stesso problema possono vedere come lo hai risolto? – JosephH

+0

Yep farà, ha dovuto aspettare per 8 ore, pubblicherà a breve. – iamsmug

risposta

32

Ecco la soluzione al mio problema, questo è molto meglio e un inferno di molto più veloce:

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

} 
]; 

spero che questo aiutare le persone.

+0

Questo mi ha aiutato molto! Eliminato il ritardo dopo il completamento del blocco. –

+0

Come posso ottenere il contenuto nel blocco "failure"? Ho ottenuto uno stato HTTP 400 nel blocco di errore con contenuto nella risposta. – TheFox

+0

operation.responseString lo farebbe. L'unico problema è che, nel mio caso, a volte il responseString è nullo (anche quando so che i dati sono stati rinviati) – elsurudo

0

La mia soluzione per HTTP POST è stato questo

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

che accoda una singola operazione sul gestore operazione in questo caso.

Problemi correlati