【问题标题】:AFNetworking, AFHTTPRequestOperation completion block slow to fire codeAFNetworking、AFHTTPRequestOperation 完成块慢触发代码
【发布时间】:2011-11-29 21:10:49
【问题描述】:

我正在使用 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.");,但任何其他代码都会在几秒钟后触发。

谁能解释一下这个问题?

【问题讨论】:

  • 我现在已经找到了解决这个问题的办法。
  • 您能否将其发布为答案,以便其他有相同问题的人可以看到您是如何解决的?
  • 可以,等了8个小时,很快就会发帖。

标签: iphone ios5 afnetworking


【解决方案1】:

这是我的问题的解决方案,这要好得多,而且要快得多:

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 状态 400。
  • operation.responseString 会做到这一点。唯一的问题是,就我而言,有时 responseString 为 nil(即使我知道数据已发回)
  • 您忘记在块末尾添加[operation start]
【解决方案2】:

我的 HTTP POST 解决方案是这样的

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

在这种情况下将单个操作排入操作管理器。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-12-17
    • 1970-01-01
    • 2015-06-17
    • 2012-06-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多