3

问题

我正在使用生成 NSURLConnection 对象的AFNetworking来上传照片。

NSURLConnection 没有对

- (void)connection:(NSURLConnection __unused *)connection
   didSendBodyData:(NSInteger)bytesWritten
 totalBytesWritten:(NSInteger)totalBytesWritten
totalBytesExpectedToWrite:(NSInteger)totalBytesExpectedToWrite

方法。(它是NSURLConnectionDataDelegate 协议的一部分,在NSURLConnection.h中定义)

该请求是带有照片附件的 HTTP POST 请求(Content-Disposition “form-data”,Mime 类型“image/jpeg”)。随着上传的进行,这应该(我认为)会导致定期调用此方法。

我需要向用户显示进度条的方法。但是,该方法没有被调用。

笔记:

  • 委托设置正确;调用其他委托方法
  • 文件上传成功

AF网络代码

我很确定这是 NSURLConnection 问题,而不是 AFNetworking 问题。

但以防万一,这是我的AFHTTPClient子类中的相关代码:

NSMutableURLRequest *request = [self multipartFormRequestWithMethod:@"POST" path:path parameters:parameters constructingBodyWithBlock:constructor];
AFHTTPRequestOperation *operation = [self HTTPRequestOperationWithRequest:request success:success failure:failure];

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

[self enqueueHTTPRequestOperation:operation];

我的 AFHTTPClient 子类不会覆盖我调用的任何方法:

  • multipartFormRequestWithMethod: path: parameters: constructingBodyWithBlock:
  • HTTPRequestOperationWithRequest: success: failure:
  • setUploadProgressBlock:
  • enqueueHTTPRequestOperation:

constructor是一个有类型的块,(void (^)(id<AFMultipartFormData>))而不是nil(通过单步执行代码验证)。

当我输入po operation.uploadProgress调试器时,我看到了,$0 = 0x1301d9f0 <__NSMallocBlock__: 0x1301d9f0>所以我认为该块设置正确。

文件总是上传成功,但是上传进度块中的代码没有运行,所以我的进度条永远不会更新。

问题

为什么 NSURLConnection 不调用这个方法?

NSURLConnection 应该在什么情况下?(它似乎没有很好的记录。)

最重要的是,如何向用户显示上传进度?

4

2 回答 2

3

A third-party error reporting library was overriding that delegate method on NSURLConnection. Removing the library resolved the issue. (There was no issue with either AFNetworking or NSURLConnection.)

If you can provide troubleshooting steps that would have isolated this issue faster, I will award the bounty to you. (Keep in mind that the third-party library is compiled, so a source code search wouldn't have worked.)

于 2013-02-15T17:25:09.797 回答
1

The library maybe implements a connection:didSendBodyData:totalBytesWritten:totalBytesExpectedToWrite: method in a category on NSObject. That would be very nasty but that could explain why the method of AFURLConnectionOperation is not called.

In order to check this assumption, you can run class-dump on your compiled binary. Search for didSendBodyData: in the output. If there’s a match in a category, the library is clearly responsible. The library could also be messing with the Objective-C runtime, but that would be harder to detect than just running class-dump.

于 2013-02-21T15:39:11.820 回答