0

我想为 api 调用制作进度条并以成功结束,我正在使用 AFNetworking 3.0 版本。

我执行以下代码来衡量进度。

NSURLSessionDataTask *obj =  [manager POST:UrlForGetAllCalEntry parameters:jsonDict progress:^(NSProgress * _Nonnull uploadProgress) {
                } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {

                        if ([[responseObject valueForKey:@"code"] integerValue] == 200)
                        {

                         }
                } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
                    [TRAVALARMMANAGER setMessage:error.localizedDescription withView:[APPDELEGATE window] textColor:txtMsgColor bgColor:bgMsgColor];
                    NSLog(@"Error: %@", error);
                }];


[manager setDataTaskDidReceiveDataBlock:^(NSURLSession * _Nonnull session, NSURLSessionDataTask * _Nonnull dataTask, NSData * _Nonnull data) {
                    if (dataTask.countOfBytesExpectedToReceive == NSURLSessionTransferSizeUnknown)
                        return;

                    if (dataTask != obj)
                        return;

                    NSUInteger code = [(NSHTTPURLResponse *)dataTask.response statusCode];

                    if (!(code> 199 && code < 400))
                        return;

                    long long  bytesReceived = [dataTask countOfBytesReceived];
                    long long  bytesTotal = [dataTask countOfBytesExpectedToReceive];

                    NSLog(@"... %lld/%lld",
                          bytesReceived,
                          bytesTotal);
                }];

但是方法从

如果(dataTask.countOfBytesExpectedToReceive == NSURLSessionTransferSizeUnknown)返回;

此语句始终返回 true。我不明白为什么?. 我还打印了标题,它有“接触长度”选项。

4

3 回答 3

0

嗯,我使用这种方法来监控下载进度,它对我来说很好用。

- (void)setDownloadTaskDidWriteDataBlock:(void (^)(NSURLSession *session, NSURLSessionDownloadTask *downloadTask, int64_t bytesWritten, int64_t totalBytesWritten, int64_t totalBytesExpectedToWrite))block {
    self.downloadTaskDidWriteData = block;
}

↓↓↓↓↓↓ 例如 ↓↓↓↓↓↓

开始下载:

NSURL *downloadURL = [NSURL URLWithString:@"example.com/file.mp4"];
NSURLRequest *request = [NSURLRequest requestWithURL:downloadURL];

self.downloadTask = [self.manager downloadTaskWithRequest:request progress:nil destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) {

    // this progress param is "downloadTask operation" progress, it's not the data receiving progress         

    return nil;
} completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) {

}];
[self.downloadTask resume];

计算下载进度:

__weak typeof(self) vc = self; 
[self.manager setDownloadTaskDidWriteDataBlock:^(NSURLSession *session, NSURLSessionDownloadTask *downloadTask, int64_t bytesWritten, int64_t totalBytesWritten, int64_t totalBytesExpectedToWrite) {

    dispatch_async(dispatch_get_main_queue(), ^{
        // here is what you want
        vc.progressView.progress = (CGFloat)totalBytesWritten / totalBytesExpectedToWrite;
    });

}];

结果:

TestProject Image 希望对您有所帮助。

于 2016-01-21T14:28:05.300 回答
0

来自Apple 文档

讨论 该值是根据从服务器接收到的 Content-Length 标头确定的。如果该标头不存在,则值为 NSURLSessionTransferSizeUnknown。

您是否尝试过避免使用该if语句?当我下载文件时,我不使用该检查,我只计算你所做的除法的进度。

于 2016-01-21T11:26:44.507 回答
-1

希望对你有帮助,你可以有很多指标类型 https://github.com/jdg/MBProgressHUD

于 2016-01-21T11:39:53.233 回答