Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to track the progress of a download using ASIHTTPRequest (ASYNC)

I am currently using an asynchronous call to my API (I setup) on my site. I am using ASIHTTPRequest's setDownloadProgressDelegate with a UIProgressView. However I don't know how I can call a selector (updateProgress) which will set a CGFloat 'progress' to the progressView's progress. I tried the following, but both the progresses were zero. Please can you tell me how I can get this working?

(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;
}
like image 684
max_ Avatar asked Dec 21 '22 12:12

max_


1 Answers

For people still finding this answer: Please note ASI is highly deprecated, you should use NSURLSession or ASIHTTPRequest instead.

One way to achieve what you want would be to set the downloadProgressDelegate to be your own class and implement setProgress:. In this implementation, update your progress variable and then call [progressView setProgress:];

Or in code, set up the request's download progress delegate:

[request setDownloadProgressDelegate:self];

and then add the method to your class:

- (void)setProgress:(float)progress
{
    currentProgress = progress;
    [progressView setProgress:progress];
}
like image 136
JosephH Avatar answered Dec 24 '22 02:12

JosephH