如何集成NSURLConnection与UIProgressView? [英] How to integrate NSURLConnection with UIProgressView?

查看:77
本文介绍了如何集成NSURLConnection与UIProgressView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试将NSURLConnection对象与UIProgressView集成,因此我可以在后台进行文件下载时更新用户。

I'm trying to integrate a NSURLConnection object with UIProgressView, so I can update the user while a file download is happening in the background.

我创建了一个单独的对象在后台下载文件,我有问题解决如何更新的UIProgressView对象的progress属性与正确的值。

I created a separate object to download the file in the background, and I'm having problems figuring out how to update the progress property in the UIProgressView object with the correct value. It's probably something very simple, but I cannot figure it out with Googling around.

这里是我有的代码:

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
    [self.resourceData setLength:0];
    self.filesize = [NSNumber numberWithLongLong:[response expectedContentLength]];
    NSLog(@"content-length: %d bytes", self.filesize);
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    [self.resourceData appendData:data];

    NSNumber *resourceLength = [NSNumber numberWithUnsignedInteger:[self.resourceData length]];
    NSLog(@"resourceData length: %d", [resourceLength intValue]);
    NSLog(@"filesize: %d", self.filesize);
    NSLog(@"float filesize: %f", [self.filesize floatValue]);
    progressView.progress = [resourceLength floatValue] / [self.filesize floatValue];
    NSLog(@"progress: %f", [resourceLength floatValue] / [self.filesize floatValue]);
}

如您所见, resourceData 保存正在下载的文件数据。 filesize 成员变量保存文件的完整大小,由我的Web服务在其Content-Length头中返回。

As you can see, the resourceData member variable holds the file data as it is being downloaded. The filesize member variable holds the full size of the file, as returned by my web service in its Content-Length header.

好的,我不断得到下载的数据与 didReceiveData 的多个执行,但是当我尝试计算进度值,没有返回正确的值。下面是我在控制台日志中获得的一个小样本:

It all works sort of OK, I keep getting the downloaded data with multiple executions of didReceiveData as I should, but when I try to calculate the progress value, no proper value is returned. See below for a small sample of what I get in my console log:

content-length: 4687472 bytes
resourceData length: 2904616
filesize: 4687472
float filesize: -1.000000
progress: -2904616.000000


b $ b

为了参考, progressView.progress 是一个浮点数。 filesize 是一个持有长长的NSNumber。最后, resourceLength 是一个NSNumber,它包含一个NSUInteger。

For reference, progressView.progress is a float. filesize is a NSNumber that holds a long long. Finally, resourceLength is a NSNumber that holds a NSUInteger.

我在这里缺少什么?

推荐答案

不知道我在这里丢失,但你的文件大小是-1似乎是你的问题。 API文档清楚地表明 expectedContentLength 可能不可用,并且在这些中返回 NSURLResponseUnknownLength 病例。 NSURLResponseUnknownLength 定义为:

Not sure what I'm missing here, but your filesize being -1 seems to be your problem. The API docs clearly state that expectedContentLength may not be available and that NSURLResponseUnknownLength is returned in these cases. NSURLResponseUnknownLength is defined as:

#define NSURLResponseUnknownLength ((long long)-1)

在这些情况下,您无法获得准确的进度。你需要处理这个并显示某种不确定的进度表。

In these cases, you cannot get an accurate progress. You'll need to handle this and display an indeterminate progress meter of some sort.

这篇关于如何集成NSURLConnection与UIProgressView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆