URLSession delegate function totalBytesExpectedToWrite always return -1

廉价感情. 提交于 2021-01-27 19:35:51

问题


I have created URLSession for downloading a file, File is being downloaded correctly no problem with that.

I want to show the percentage count of remain bytes, but the delegate function:

-(void) URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didWriteData:(int64_t)bytesWritten totalBytesWritten:(int64_t)totalBytesWritten totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite

and its parameter totalBytesExpectedToWrite always returns -1.

All thing was working fine before few days, there is no change with code but it suddenly stopped sending Expected bytes.

My request code is like:

NSURLSessionConfiguration *sessionConfig = [NSURLSessionConfiguration defaultSessionConfiguration];

NSURLSession *session = [NSURLSession sessionWithConfiguration:sessionConfig delegate:self delegateQueue:nil];

NSMutableURLRequest*request = [NSMutableURLRequest requestWithURL:fileUrl];

NSDictionary*param = [[NSDictionary alloc]initWithObjectsAndKeys:@"",@"Accept-Encoding", nil];
[request setAllHTTPHeaderFields:param];

NSURLSessionDownloadTask *downloadTask = [session downloadTaskWithRequest:request];
[downloadTask resume];

is there any change with the api which i am missing? OR any other way around?


回答1:


-1 is NSURLSessionTransferSizeUnknown, which means that the http server did not provide a "Content-Length" header (and the data is sent using "Transfer-Encoding: chunked").

There is probably not much that you can do. You could try if the workaround from https://stackoverflow.com/a/12599242/1187415 works in your case as well:

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:anURL];
[request addValue:@"" forHTTPHeaderField:@"Accept-Encoding"];


来源:https://stackoverflow.com/questions/52698264/urlsession-delegate-function-totalbytesexpectedtowrite-always-return-1

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!