使用UIProgressView和ASIHTTPRequest跟踪下载进度

时间:2012-03-18 05:47:24

标签: iphone objective-c asihttprequest

我正在使用ASINetworkQueue下载一系列要在特定视图中显示的图像。

Mi问题是当我打开视图然后立即关闭它,在请求完成之前,尝试更新UIProgressView需要一段时间,然后它崩溃(EXC_BAD_ACCESS),因为它不再存在(该观点被驳回)。我尝试在卸载视图之前将委托设置为nil,但它没有帮助。

这是负责创建队列和添加请求

的代码的一部分
- (void)doNetworkOperations
{
    // Stop anything already in the queue before removing it
    [[self networkQueue] cancelAllOperations];

    // Creating a new queue each time we use it means we don't have to worry about clearing delegates or resetting progress tracking
    [self setNetworkQueue:[ASINetworkQueue queue]];
    [[self networkQueue] setDelegate:self];

    // if I have little files to download, use accurate progress
    // if I have lots, it's not worth it.
    if ([promosInfo count]<=4) {
        [[self networkQueue] setShowAccurateProgress:YES];
    }
    [[self networkQueue] setDownloadProgressDelegate:progressControl];
    [[self networkQueue] setRequestDidFinishSelector:@selector(requestFinished:)];
    [[self networkQueue] setRequestDidFailSelector:@selector(requestFailed:)];
    [[self networkQueue] setQueueDidFinishSelector:@selector(queueFinished:)];

    NSInteger i=0;
    for (id row in promosInfo){
        NSURL *url = [NSURL URLWithString: [row objectForKey:@"imgSource"]];
        ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL: url];
        [request setTag:i];
        [[self networkQueue] addOperation:request];
        i++;
        url = nil;
        request = nil;
    }

    [[self networkQueue] go];
}

我有以下viewDidUnload

- (void)viewDidUnload
{
    [[self networkQueue] setDownloadProgressDelegate:nil];
    [[self networkQueue] cancelAllOperations];
    [self setNetworkQueue:nil];
    [self setCargando:nil];
    [self setScrollView:nil];
    [self setPageControl:nil];
    [self setStatusLabel:nil];
    [self setProgressControl:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

如何告诉ASINetworkQueue停止跟踪进度?正如我所提到的,我尝试将委托设置为nil,但它似乎没有用。

3 个答案:

答案 0 :(得分:0)

我已经完成了以下代码并实现了进度条的功能......

  1. 在我打算下载并保存progressBar的类中,键的值为字典中每个请求的url不同,然后将该字典保存到类名Singleton中。

        UIProgressView *_progressBar = [[UIProgressView alloc] init];
        ASIHTTPRequest *request;
        request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:trimmedString]];
        [request setDelegate:self];
        [request setDownloadDestinationPath:filePath];
        [request setDownloadProgressDelegate:_progressBar];
        [request setShowAccurateProgress:YES];
        [request setShouldContinueWhenAppEntersBackground:YES];
        [request setDidFinishSelector:@selector(downloadedSuccessfully:)];
        [request setDidFailSelector:@selector(downloadedFailure:)];
        [[[MySingleton sharedSingleton ]getProgressValue] setValue:_progressBar forKey:trimmedString];
    
  2. 然后在我将要显示进度条的视图控制器中,我编写了以下代码:它从Singleton类的字典中读取每个单元格的进度条的值,并在表格中显示。如果完成它不显示因为在dic中值将为nil,但你必须为此重新加载表。

    UIProgressView *progressBar = (UIProgressView *)[[[MySingleton sharedSingleton] getProgressValue] valueForKey:[dictionary valueForKey:@"SongUrl"]];
        [progressBar setFrame:CGRectMake(215, 27, 40, 10)];
        [cell.contentView addSubview:progressBar];
    

答案 1 :(得分:0)

尝试在viewDidUnload中添加此代码以查看其是否有效。

for (ASIFormDataRequest *req in [networkQueue operations]) {
    [req cancel];
    [req setDelegate:nil];
}

[networkQueue reset];
[networkQueue cancelAllOperations];
[networkQueue setDelegate:nil];

答案 2 :(得分:-2)

我终于通过更改取消请求,将委托设置为nil以及从ViewDidLoad到ViewWillDissapear:(BOOL)动画来实现它的工作。