如何使用ASIHTTPRequest(ASYNC)跟踪下载进度

时间:2011-06-12 13:06:40

标签: iphone objective-c asihttprequest uiprogressview

我目前正在我的网站上使用异步调用API(我设置)。我正在使用ASIHTTPRequest的setDownloadProgressDelegate和UIProgressView。但是我不知道如何调用一个选择器(updateProgress),它将把一个CGFloat'progress'设置为progressView的进度。我尝试了以下,但两个进展都是零。请告诉我如何才能使这个工作?

(in some method)

ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:[url stringByAppendingFormat:@"confidential"]]];
    [request setDownloadProgressDelegate:progressView];
    [NSTimer scheduledTimerWithTimeInterval:1.0f/60.0f target:self selector:@selector(updateProgress:) userInfo:nil repeats:YES];
    [request setCompletionBlock:^{~100 lines of code}];
    [request setFailedBlock:^{~2 lines of code :) }];
    [request startAsynchronous];

- (void) updateProgress:(NSTimer *)timer {
    if (progressView.progress < 1.0) {
        currentProgress = progressView.progress;
        NSLog(@"currProg: %f --- progressViewProg: %f", currentProgress, progressView.progress);
    }
    else {
        [timer invalidate];
    }
    return;
}

3 个答案:

答案 0 :(得分:7)

对于仍然找到此答案的人:请注意ASI被高度弃用,您应该使用NSURLSession或ASIHTTPRequest。

实现目标的一种方法是将downloadProgressDelegate设置为您自己的类并实现setProgress:。在此实施中,更新您的进度变量,然后调用[progressView setProgress:];

或者在代码中,设置请求的下载进度委托:

[request setDownloadProgressDelegate:self];

然后将该方法添加到您的班级:

- (void)setProgress:(float)progress
{
    currentProgress = progress;
    [progressView setProgress:progress];
}

答案 1 :(得分:4)

尝试添加到您的请求中:

[request setShowAccurateProgress:YES];

它无法帮助您致电updateProgressASIHTTPRequest会改变进度指示器本身。

答案 2 :(得分:0)

下载东西时,BTW:NS*Connection appears to be quite a bit faster than ASI*

在任何情况下,example on this page都意味着您不需要手动将下载对象中的值“复制”到进度视图对象。

实际上,基于计时器的代码正在从进度视图中获取进度,进度视图应该已经显示进度。如果我正确理解ASIHTTP *,则根本不需要在此代码中使用计时器。