2011-11-29 21 views
9

に遅いAFHTTPRequestOperation完了ブロックが、それはすべての[OK]を動作しますが、次のブロックの上に私はいくつかの問題を持っている:AFNetworking、火災コード

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

は基本的に私私のサーバー側のスクリプトは、いくつかの検証を行いますHTTPステータスコードを返します(421は有効ではありません)。これにより、サーバー上で何が問題になったのかを知ることができます。

私の問題は、応答が戻って来るとき、それはすぐにNSLog(@"success");またはNSLog(@"Username taken.");を発射するが、かなりの数秒のいずれかの他のコードの火災後のことです。

誰でもこの光を放つことはできますか?ここで

+0

私は今この問題の解決策を見つけました。 – iamsmug

+1

あなたは答えとしてそれを投稿できますか?同じ問題を抱えている他の人があなたの解決方法を見ることができますか? – JosephH

+0

ええ、8時間待たなければならない、すぐに投稿されます。 – iamsmug

答えて

32

が、これはより速くより良いとたくさんの地獄で、私の問題を解決する:

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

} 
]; 

私はこのヘルプの人々を願っています。 HTTP POSTのための

+0

これは私をたくさん助けました!ブロックが完了してからの遅延をなくしました。 –

+0

「失敗」ブロックのコンテンツを取得するにはどうすればよいですか?私は、レスポンスの内容を含む失敗ブロックでHTTP Status 400を取得しました。 – TheFox

+0

operation.responseStringはそれを行います。 (データが返ってきたことがわかっていても)問題があるだけで、時にはresponseStringがnilであることがあります。 – elsurudo

0

私のソリューションは、この場合には運用管理上の単一の操作をキューに登録この

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

ました。

関連する問題