1

I am trying to download a file using AFNetworking 2.0 using the following piece of code

AFHTTPRequestSerializer *serialize = [AFHTTPRequestSerializer new];
[serialize setAuthorizationHeaderFieldWithUsername...// set the auth header with username and password
serialize.timeoutInterval = 200;
NSError *error = nil;
NSMutableURLRequest *request = [serialize requestWithMethod:@"GET" URLString:url parameters:nil error:&error];
AFHTTPSessionManager *session = [AFHTTPSessionManager manager];
NSProgress *progress = nil;

NSURLSessionDownloadTask *downloadTask = [session downloadTaskWithRequest:request progress:&progress destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) {
    NSLog(@"success….”);
    NSURL *url = [NSURL new];
    return url;
} completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) {
    NSLog(@"error: %@", error.localizedDescription);
    }];

[progress addObserver:self forKeyPath:@"fractionCompleted" options:NSKeyValueObservingOptionNew context:NULL];
[downloadTask resume];
}

// Adding the observer here for progress

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{

dispatch_async(dispatch_get_main_queue(), ^{
    if ([keyPath isEqualToString:@"fractionCompleted"]) {
        NSProgress *progress = (NSProgress *)object;
        NSLog(@"Progress… %f total unit count =%lld , completed unit count =%lld", progress.fractionCompleted , progress.totalUnitCount,progress.completedUnitCount);
    } else {
        [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
    }
});

}

And here is the NSLog from observeValueForKeyPath method

SampleDownload[5833:149756] Progress… 0.000000 total unit count =-1 , completed unit count =0
SampleDownload[5833:149756] Progress… 0.000000 total unit count =-1 , completed unit count =975
SampleDownload[5833:149767] Progress… 0.000000 total unit count =-1 , completed unit count =25149
SampleDownload[5833:149767] Progress… 0.000000 total unit count =-1 , completed unit count =31086
SampleDownload[5833:149767] Progress… 0.000000 total unit count =-1 , completed unit count =53006
SampleDownload[5833:149754] Progress… 0.000000 total unit count =-1 , completed unit count =54377
SampleDownload[5833:149755] Progress… 0.000000 total unit count =-1 , completed unit count =66098
SampleDownload[5833:149768] Progress… 0.000000 total unit count =-1 , completed unit count =93626
SampleDownload[5833:149763] Progress… 0.000000 total unit count =-1 , completed unit count =103805

So as you can see total unit count I am getting is -1. As a result of that I think fractionCompleted is 0 and I am not able to update the UI. What is the mistake that I am making ?

I have used this as a reference. How to get download progress in AFNetworking 2.0?

Also there is some similar discussion happening on this thread but I was not able figure out the reason. https://github.com/AFNetworking/AFNetworking/issues/1398

EDIT

Posting the response header

{
"Access-Control-Allow-Credentials" = true;
"Access-Control-Allow-Headers" = "Accept,Authorization,Cache-Control,Content-Type,DNT,If-Modified-Since,Keep-Alive,Origin,User-Agent,X-Mx-ReqToken,X-Requested-With";
"Access-Control-Allow-Methods" = "GET, POST, PUT, DELETE, OPTIONS";
"Access-Control-Allow-Origin" = "*";
"Cache-Control" = "public, max-age=43200";
Connection = "keep-alive";
"Content-Encoding" = gzip;
"Content-Length" = 1166764;
"Content-Type" = "application/pdf";
Date = "Wed, 17 Dec 2014 12:17:43 GMT";
Etag = “someEtag”;
Expires = "Thu, 18 Dec 2014 00:17:43 GMT";
"Last-Modified" = "Wed, 17 Dec 2014 11:58:31 GMT";
Server = "nginx/1.4.6 (Ubuntu)";
Vary = "Accept-Encoding";
}
Community
  • 1
  • 1
shshnk
  • 1,621
  • 14
  • 26
  • Check if server returns `Content-Length` – pronebird Dec 17 '14 at 13:33
  • @Andy Thanks! updated the question details with the response header that I am getting. – shshnk Dec 17 '14 at 13:39
  • Hmm I don't know, all seems legit to me.. – pronebird Dec 17 '14 at 14:10
  • See http://stackoverflow.com/questions/9550971/afnetworking-progress and http://stackoverflow.com/questions/7417610/why-response-expectedcontentlength-always-return-1/7426735#7426735 (and bear in mind that when searching for this in Google, if you include `-1` in your search terms, I think it *excludes* all pages which mention the number 1, whether positive or negative! `-` is Google's ["Remove words" operator](https://support.google.com/websearch/answer/2466433?p=adv_operators&hl=en&rd=1).) – Matt Gibson Dec 19 '14 at 16:02

1 Answers1

1

The problem may be the Content-Encoding of gzip, which can cause the NSURLSessionDownloadDelegate method didWriteData to return -1 for the total bytes expected. You can try disabling gzip by setting Accept-Encoding before issuing the request:

[request setValue:@"" forHTTPHeaderField:@"Accept-Encoding"];
Rob
  • 415,655
  • 72
  • 787
  • 1,044