【问题标题】:Make a progress bar for get the response with AFNetworking 3.0制作进度条以获取 AFNetworking 3.0 的响应
【发布时间】:2016-01-21 11:05:58
【问题描述】:

我想为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。我不明白为什么? .我还打印了标题,它有“联系长度”选项。

【问题讨论】:

    标签: ios objective-c progress-bar afnetworking-3


    【解决方案1】:

    来自Apple Docs

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

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

    【讨论】:

    • 我尝试注释该代码,然后我在 [dataTask countOfBytesExpectedToReceive] 中得到 -1;这个方法
    • 我尝试使用您的解决方案链接,但每次都得到 expectedContentLength = -1。 api端需要改吗?
    【解决方案2】:

    嗯,我用这个方法来监控下载进度,对我来说效果很好。

    - (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 希望能帮助到你。

    【讨论】:

    • 我没有下载任何东西,我只想同步日历事件并获得回复。不需要下载任何东西
    【解决方案3】:

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

    【讨论】:

    • 我不要指标。我想要有下载效果的进度条。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-19
    • 1970-01-01
    • 2017-06-09
    • 2017-02-13
    • 2017-04-02
    • 1970-01-01
    相关资源
    最近更新 更多