NSURLConnection(作为AFNetworking的一部分)不会调用NSURLConnectionDataDelegate委托 [英] NSURLConnection (as part of AFNetworking) doesn't call NSURLConnectionDataDelegate delegate

查看:56
本文介绍了NSURLConnection(作为AFNetworking的一部分)不会调用NSURLConnectionDataDelegate委托的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题



我正在使用 AFNetworking ,它会生成一个NSURLConnection对象,以上传照片。



NSURLConnection不会对



<$进行任何调用p $ p> -(void)连接:(NSURLConnection __unused *)连接
didSendBodyData:(NSInteger)bytesWrited
totalBytesWritten:(NSInteger)totalBytesWrited
totalBytesExpectedToWrite:(NSInteger )totalBytesExpectedToWrite

方法。 (这是 NSURLConnectionDataDelegate协议,在NSURLConnection.h中定义)



该请求是带有照片附件的HTTP POST请求(Content-Disposition form-data,MIME类型为 image / jpeg )。 (我认为)这会导致在上载进度时定期调用此方法。



我需要该方法向用户显示进度条。但是,不会调用该方法。



注意:




  • 委托设置正确;其他委托方法称为

  • 文件上传成功



AFNetworking代码



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



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

  NSMutableURLRequest * request = [self multipartFormRequestWithMethod:@ POST路径:路径参数:参数constructionsBodyWithBlock:constructor]; 
AFHTTPRequestOperation * operation = [self HTTPRequestOperationWithRequest:请求成功:成功失败:失败];

if(constructor){
[operation setUploadProgressBlock:^(NSUInteger bytesWrited,long long totalBytesWritten,long long totalBytesExpectedToWrite){
NSLog(@已发送%lld个%lld个字节,totalBytesWritten,totalBytesExpectedToWrite);
}];
}

[self enqueueHTTPRequestOperation:operation];

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




  • multipartFormRequestWithMethod:路径:参数:ConstructingBodyWithBlock:

  • HTTPRequestOperationWithRequest:成功:失败:

  • setUploadProgressBlock:

  • enqueueHTTPRequestOperation:



构造函数是类型为(void(^)(id< AFMultipartFormData>))的块,而不是 nil (通过单步执行代码进行验证)。



当我在调试器中键入 po operation.uploadProgress 时,我参见 $ 0 = 0x1301d9f0< __ NSMallocBlock__:0x1301d9f0> ,所以我认为该块已正确设置。



该文件总是成功上传,但是上传进度块中的代码未运行,因此我的进度栏永远不会更新。



问题



为什么NSURLConnection不调用此方法?



NSURLConnection应该在什么情况下出现? (似乎没有充分的文档记录。)



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

解决方案

该库可能在​​<上的类别中实现 connection:didSendBodyData:totalBytesWritten:totalBytesExpectedToWrite:方法code> NSObject 。那会很讨厌,但这可以解释为什么不调用 AFURLConnectionOperation 方法。



按照这种假设,您可以在编译后的二进制文件上运行 class-dump 。在输出中搜索 didSendBodyData:。如果某个类别中有匹配项,则图书馆应明确负责。该库也可能使Objective-C运行时陷入混乱,但这比仅运行类转储更难检测。


The Issue

I am using AFNetworking, which generates an NSURLConnection object, to upload a photo.

The NSURLConnection is not making any calls to the

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

method. (It's part of the NSURLConnectionDataDelegate protocol, defined in NSURLConnection.h)

The request is an HTTP POST request with a photo attachment (Content-Disposition "form-data", Mime type "image/jpeg"). This should (I think) result in periodic calls to this method as the upload progresses.

I need the method to display a progress bar to the user. However, the method is not getting called.

Note:

  • The delegate is set properly; other delegate methods are called
  • The file upload is successful

AFNetworking Code

I'm pretty sure this is an NSURLConnection issue, not an AFNetworking issue.

But just in case, here is the relevant code from my AFHTTPClient subclass:

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

My AFHTTPClient subclass does NOT override ANY of the methods I am calling:

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

constructor is a block with type (void (^)(id<AFMultipartFormData>)) and is not nil (verified by stepping through code).

When I type po operation.uploadProgress in the debugger, I see $0 = 0x1301d9f0 <__NSMallocBlock__: 0x1301d9f0> so I think the block is being set correctly.

The file always uploads successfully, but the code in the upload progress block is not run, so my progress bar never updates.

Questions

Why isn't NSURLConnection calling this method?

What are the circumstances in which NSURLConnection is supposed to? (It doesn't appear to be well-documented.)

And most importantly, how can I display the upload progress to the user?

解决方案

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.

这篇关于NSURLConnection(作为AFNetworking的一部分)不会调用NSURLConnectionDataDelegate委托的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆