AFNetworking - AFHTTPRequestOperation - 检查setUploadProgressBlock的失败?

时间:2013-05-30 16:01:33

标签: ios objective-c afnetworking

我可以将图像发送到服务器,但我遇到了问题。 我可以用这样的简单块检查失败:

AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"success: %@", operation.responseString);
}
failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"error: %@",  operation.responseString);
    }
 ];
[operation start];

但我无法看到使用此块发送的进度。所以我找到了一个特殊的块,其中包含进度回调:

[operation setUploadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {
    NSLog(@"Sent %lld of %lld bytes", totalBytesWritten, totalBytesExpectedToWrite);
}];
[httpClient enqueueHTTPRequestOperation:operation];

问题是使用setUploadProgressBlock没有“失败:”......

所以我的问题是......有一种方法可以检查发送是否失败?

感谢您的帮助

2 个答案:

答案 0 :(得分:1)

这样可以正常工作,如果出现问题,它会提供进展并进入失败的阻止:

NSURLRequest *request = [[NSURLRequest alloc] init];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
 NSLog(@"success: %@", operation.responseString);
 }
 failure:^(AFHTTPRequestOperation *operation, NSError *error) {
 // If access token is null , we ask to the user if he wants to sign in or sign up ????
 NSLog(@"error: %@",  operation.responseString);
 }
 ];
 [operation setUploadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {
 NSLog(@"Sent %lld of %lld bytes", totalBytesWritten, totalBytesExpectedToWrite);
 }];
[operation start];

答案 1 :(得分:0)

您可以设置两个块并实现您想要的效果。