user1597438
user1597438

Reputation: 2231

Adding a download progress bar using ASIHTTPRequest created on a NSObject class

I have a global NSObject class where I added an ASIHTTPRequest function to update my application. I have set the UIProgressView on a ViewController class. The NSObject class looks like so:

+(void)updateAllWithCategory:(NSMutableArray *)categories{
for(NSMutableArray *item in categories) {
    NSString *urlString = [NSString stringWithFormat:@"http://mywebsite.com/%@.xml", item];
    NSURL *url = [[NSURL alloc]initWithString:urlString];
    __block __weak ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
    [request setShowAccurateProgress:YES];
    [request setDownloadProgressDelegate:self];
    [request setTimeOutSeconds:20];
    [request setCompletionBlock:^{
        //download all data and show alert
    }];
    [request setFailedBlock:^{
        //display all errors
    }];
    [request startAsynchronous];
}}

float bytesReceived = 0;
float totalSize = 0;
float estimate = 0;

+(void)request:(ASIHTTPRequest *)request didReceiveBytes:(long long)bytes {

bytesReceived = bytesReceived + (float)bytes;
float dlSize = (float)[request contentLength];
totalSize = totalSize + dlSize;

estimate = (bytesReceived / (totalSize * 1.0f) * 100);

//write estimate in plist with value estTime
}

Then on my view controller I use the said function like so:

-(void)updateChecker {
//get estTime from plist
[GlobalClass updateAllWithCategory:myArray]
progressbar.hidden = NO;
[progressbar setProgress:estTime animated: YES];
progressbar.progress = estTime;
}

updateChecker is called via a NSTimer set on viewDidLoad. Neither setProgress:animated: nor .progress worked. My progress bar does not move even a little. So my question is, how exactly do you implement a progress bar for an ASIHTTPRequest that's called from a global NSObject class?

Upvotes: 0

Views: 532

Answers (1)

mak
mak

Reputation: 1183

You need to create a UIProgressView.

UIProgressView *progressView = [UIProgressView alloc] init];

and then replace this

  [request setDownloadProgressDelegate:self];

with this

[request setDownloadProgressDelegate:progressView];

Upvotes: 2

Related Questions