Search code examples
iosxcodensmutablearrayasihttprequestuiprogressview

How to remove the associated UIProgressview with the right request?


I use asihttprequest to download multiple files and I'm wondering how I can remove the associated UIProgressview with the right request when the download is done.

NSMutableArray *contentArray contains the ASIHTTPRequest and NSMutableArray *progArray contains my custom UIProgressview.

-(void)addDownload:(NSString *)theURL withName:(NSString *)fileName
{
theProgress = [[PDColoredProgressView alloc] initWithFrame:CGRectMake(3, 17, 314, 14)];
//...
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:theURL]];
//..
[request setDelegate:self];
    [request setDownloadProgressDelegate:theProgress];
    request.allowResumeForFileDownloads = YES;
    [request startAsynchronous];
    [request setShouldContinueWhenAppEntersBackground:YES];
    [contentArray addObject:request];
    [progArray addObject:theProgress];
    [theProgress retain];

    [self.tableView reloadData];
}

- (void)requestFinished:(ASIHTTPRequest *)request{

        [contentArray removeObject:request]; 
        [progArray removeObject:theProgress]; 
        NSLog(@"%@",progArray);
        NSLog(@"%@",contentArray);
        [self reloadMyData]; 
        [self.tableView reloadData]; 


        }

The problem is that this code remove the last progressview even if the there are 3 downloads in contentArray and the second one finish first. Can you help me with this ?


Solution

  • If you need to remove progress view that's associated with finished request you can get it from request's downloadProgressDelegate property:

    - (void)requestFinished:(ASIHTTPRequest *)request{
    
        PDColoredProgressView *progress = (PDColoredProgressView*)request.downloadProgressDelegate;
        [contentArray removeObject:request]; 
        if (progress)
           [progArray removeObject:progress]; 
        [self reloadMyData]; 
        [self.tableView reloadData]; 
    }