NSURLConnection (as part of AFNetworking) does not call NSURLConnectionDataDelegate delegate
Problem
I am using AFNetworking , which generates an NSURLConnection object to load a photo.
NSURLConnection does not make any calls to
- (void)connection:(NSURLConnection __unused *)connection
didSendBodyData:(NSInteger)bytesWritten
totalBytesWritten:(NSInteger)totalBytesWritten
totalBytesExpectedToWrite:(NSInteger)totalBytesExpectedToWrite
method. (This is part of the NSURLConnectionDataDelegate protocol defined in NSURLConnection.h)
The request is an HTTP POST request with photos attached (Content-Disposition "form-data", Mime type "image / jpeg"). This should (I think) result in periodic calls to this method as the download progresses.
I need a method to display a progress bar to the user. However, the method was not called.
Note:
- The delegate is set correctly; other delegate methods are called
- File upload successfully
AFNetworking code
I'm pretty sure this is an NSURLConnection issue and not an AFNetworking issue.
But just in case, here's the relevant code from my subclass 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];
My AFHTTPClient subclass DOES NOT override ANY of the methods I call:
-
multipartFormRequestWithMethod: path: parameters: constructingBodyWithBlock:
-
HTTPRequestOperationWithRequest: success: failure:
-
setUploadProgressBlock:
-
enqueueHTTPRequestOperation:
constructor
is a block with a type (void (^)(id<AFMultipartFormData>))
and not nil
(checked by going through the code).
When I type po operation.uploadProgress
in the debugger I see $0 = 0x1301d9f0 <__NSMallocBlock__: 0x1301d9f0>
, so I think the block is set up correctly.
The file always uploads successfully, but the code in the upload progress block does not run, so the progress bar is never updated.
Questions
Why is NSURLConnection not calling this method?
What are the circumstances in which NSURLConnection is intended? (It does not appear to be documented.)
Most importantly, how can I show the download progress to the user?
source to share
The library can implement a method connection:didSendBodyData:totalBytesWritten:totalBytesExpectedToWrite:
in a category on NSObject
. It would be very frustrating, but it could explain why the method is AFURLConnectionOperation
not being called.
To test this assumption, you can run class-dump on your compiled binary. Find didSendBodyData:
at the exit. If theres a match in a category, the library has a clear responsibility. The library can also interact with the Objective-C environment, but that would be more difficult to detect than just dumping the class.
source to share
A third party error reporting library has overridden this delegation method in NSURLConnection. Removing the library resolved the issue. (There was no problem with AFNetworking or NSURLConnection.)
If you can provide troubleshooting steps to isolate this problem more quickly, I will reward you for a reward. (Be aware that the third party library is compiled, so the source code search did not work.)
source to share