Tôi gặp phải vấn đề lạ. Tôi tải tệp từ Internet bằng cách sử dụng NSURLSession
và NSURLSessionDownloadTask
. Đây là mã sốtotalBytesExpectedToWrite là -1 trong NSURLSessionDownloadTask
NSURLSessionConfiguration *sessionConfiguration =
[NSURLSessionConfiguration backgroundSessionConfiguration:kSessionId];
self.session = [NSURLSession sessionWithConfiguration:sessionConfiguration
delegate:self
delegateQueue:[NSOperationQueue new]];
NSURL *url = [NSURL URLWithString:urlString];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
NSURLSessionDownloadTask *downloadTask = [self.session downloadTaskWithRequest:request];
[downloadTask resume];
Lớp học của tôi được khai báo là NSURLSessionDownloadDelegate
và tôi nhận được cuộc gọi lại tốt. Nhưng khi hệ thống gọi là phương pháp đại biểu
- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didWriteData:(int64_t)bytesWritten totalBytesWritten:(int64_t)totalBytesWritten totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite
{
NSLog(@"totalBytesExpectedToWrite: %lld", totalBytesExpectedToWrite);
NSLog(@"%lld", totalBytesWritten);
}
totalBytesExpectedToWrite
luôn bằng -1
và tôi không có khả năng hiển thị một tiến trình để sử dụng bởi vì tôi không biết kích thước tập tin tải xuống của.
Bạn có thể nhắc tôi đã nhầm lẫn không?
Nó có thể là một vấn đề máy chủ nơi đó không được gửi 'Content-Length' tiêu đề . Nếu bạn sử dụng cùng một URL trong trình duyệt, trình duyệt của bạn có hiển thị đúng tiến trình không? – rmaddy