4

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;
}
max_
  • 24,076
  • 39
  • 122
  • 211
  • Can you explain what you're actually trying to get the code to do - do you want to have a progress view and also want to know about progress in your own class? – JosephH Jun 12 '11 at 18:17
  • I want to be able to store a CGFloat called progress which will be updated when a method in the class is called. It will be updated by the ASIHTTPRequest in that method. – max_ Jun 12 '11 at 18:47

3 Answers3

7

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];
}
JosephH
  • 37,173
  • 19
  • 130
  • 154
4

Try to add in to your request:

[request setShowAccurateProgress:YES];

It won't help you to call updateProgress, ASIHTTPRequest will change progress indicator itself.

axel22
  • 32,045
  • 9
  • 125
  • 137
VenoMKO
  • 3,294
  • 32
  • 38
0

BTW: NS*Connection appears to be quite a bit faster than ASI* when downloading stuff.

In any case, the example on this page implies that you don't need to manually "copy" the value from the the download object to the progress view object.

In fact, your timer based code is getting the progress from the progress view which should be showing the progress already. There should be no need for a timer in this code at all, if I understand ASIHTTP* correctly.

bbum
  • 162,346
  • 23
  • 271
  • 359
  • btw: There's some newer performance data comparing ASI & NSURLConnection here: http://allseeing-i.com/ASIHTTPRequest-1.5 – JosephH Jun 12 '11 at 18:14
  • 1
    Oh -- cool -- looks like the gap narrowed. Good stuff! I would still lean to NS* unless the additional capabilities of ASI* led directly to significantly fewer lines of code in my app. – bbum Jun 12 '11 at 18:20