I have created a subclass of AFHTTPSessionManager:
class TBNetworkClient: AFHTTPSessionManager {
func wordsWithCompletionBlock(completionBlock: TBErrorHandler) {
GET("words", parameters: nil, progress: { progress in
print("----------") //this is not called at all, why?
}, success: { sessionDataTask, response in
print((sessionDataTask.response as? NSHTTPURLResponse)?.allHeaderFields)
completionBlock(nil) //success is called
}) { sessionDataTask, error in
completionBlock(error)
}
}
}
Marked line is not called at all, why?
The output in success is following:
Optional([Server: Apache, Connection: Keep-Alive, Content-Type: text/html, Date: Fri, 08 Jan 2016 08:58:18 GMT, Content-Encoding: gzip, Keep-Alive: timeout=1, max=100, Content-Length: 5663, Vary: Accept-Encoding,User-Agent])
environment: Xcode 7 and iOS 9.0, latest version of AFNetworking from CocoaPods
NSURLSessionTasks only report progress if the Content-Length header is present, which it appears to be there based on your response.
Could you include the URL you are trying to hit? We have unit tests around this feature, so you can see that it _is_ working. I would assume something is going on with the response from the server, and start looking there.
Of course: http://www.taboo.blue-world.pl/api/words
but you must additionally set two headers to get the response:
setValue("normal", forHTTPHeaderField: "X-Type")
setValue("pl", forHTTPHeaderField: "X-Language")
setValue("2", forHTTPHeaderField: "X-Level")
iOS8.0, 9.0 is OK, but progress block not called at all锛宨OS7.0
AFURLSessionManager *manager = [EHDownloadNetwork sharedInstance].manager;
downloadTask.task = [manager downloadTaskWithRequest:request
progress:^(NSProgress * _Nonnull downloadProgress) {
if (progressBlock) {
dispatch_async(dispatch_get_main_queue(), ^{
progressBlock(downloadProgress.fractionCompleted);
});
}
@emilyZhouwm
Can you provide more information about the response the server is sending? AFNetworking hooks into the NSURLSession progress update properties, which rely on a response to properly format the expected amount of bytes to be returned by the response.
@kcharwood do you know more about how to solve this issue?
The tl;dr; version is AFNetworking only gets progress callbacks if NSURLSession gets progress callbacks. NSURLSession relies on properly formatted response headers to help determine how many bytes are left to be downloaded. AFNetworking is simply wrapping KVO of the session data task.
So it is impossible to make progress working with simple GET?
No. Progress does work with a simple GET. There are many tests in the test suite showing that. But it does require the server to return proper response headers to support it at the NSURLSession level.
So, could you help me investigate what is wrong with my response?
I'd start by looking at the test suite, and checking that out. Those tests hit httpbin.org, which is public and you can see all the headers returned. I'd then compare and contrast those with the headers you get back, and see if we can pick anything out.
Ok, So I need to wait then... no problem. If I could help you somehow, let me know. I think that it is quite possible that some headers are wrong or do not exists at all on my server response.
I just pushed in a patch from @emilyZhouwm that fixes this for download tasks on iOS 7.
At this point, I'm not aware of any other issues. If someone can provide a clear example of HTTP response headers for a request where progress is not working, we can move forward on debugging any other progress related issues.
:beers:
Will it be available through CocoaPods also?
It will be available in the 3.1.0 release of AFNetworking, and is currently available in master.
@kcharwood
I have the same issue and I'm using the latest version of AFNetworking 3.1.0 and iOS 9.3,
downloadProgress does not get called under downloadTaskWithRequest:
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];
NSURL *URL = [NSURL URLWithString:@"https://s3.amazonaws.com/awdtest/fullzip.pdf"];
NSURLRequest *request1 = [NSURLRequest requestWithURL:URL];
NSURLSessionDownloadTask *downloadTask1 = [manager downloadTaskWithRequest:request1 progress:^(NSProgress * _Nonnull downloadProgress)
{
NSLog(@"Progress: %f", downloadProgress.fractionCompleted);
} destination:^NSURL *(NSURL *targetPath, NSURLResponse *response)
{
NSURL *documentsDirectoryURL = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil];
return [documentsDirectoryURL URLByAppendingPathComponent:[response suggestedFilename]];
} completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error)
{
NSLog(@"File downloaded to: %@", filePath);
}];
[downloadTask1 resume];
Response headers:
HTTP/1.1 200 OK
x-amz-id-2: CW06YcgIycOHZQy8bCJrT3aNfhatM9pty1mOjgYHumjCxRmNAQ+jhJHRQwl7mDIaQeTHI0fyrnU=
x-amz-request-id: 105E010DECC91897
Date: Sat, 14 May 2016 09:26:38 GMT
Content-Encoding: gzip
Last-Modified: Sat, 14 May 2016 09:21:29 GMT
ETag: "88bbe0b318bf11dd56a31176d3384e78"
Accept-Ranges: bytes
Content-Type: application/pdf
Content-Length: 1243325
Server: AmazonS3
it works if I use this file which is not gzipped: https://s3.amazonaws.com/awdtest/full.pdf
Response headers:
HTTP/1.1 200 OK
x-amz-id-2: gFOVfhheaMdeJOBb+7H8oaXjLLeOqlQl616XnYx6C2Gj7PBVLKZ9kMIN2fJOrGBcSgQ/7nbQOc0=
x-amz-request-id: 26669D9B576E300A
Date: Sat, 14 May 2016 09:54:58 GMT
Last-Modified: Fri, 16 May 2014 05:42:35 GMT
ETag: "6f16d7e09023ce8b50fd67abba7825c4"
Accept-Ranges: bytes
Content-Type: application/pdf
Content-Length: 1411131
Server: AmazonS3
Most helpful comment
@kcharwood
I have the same issue and I'm using the latest version of AFNetworking 3.1.0 and iOS 9.3,
downloadProgress does not get called under downloadTaskWithRequest:
Response headers:
it works if I use this file which is not gzipped: https://s3.amazonaws.com/awdtest/full.pdf
Response headers: