即使我导航到另一个viewcontroller,如何使用ASIHTTPRequest保持下载进度

时间:2012-04-27 13:29:43

标签: iphone multithreading download asihttprequest background-process

我想使用ASIHTTPRequest执行多个下载过程,并使用代码

实现

修改

- (void)download{
    UIImageView *image = (UIImageView *)[mainView viewWithTag:selectedTag];

    for (UIProgressView *currentProgress in [image subviews]) {
        if ([currentProgress isKindOfClass:[UIProgressView class]]) {
            if(currentProgress)
            {
                currentProgress.progress = 0.0;
                ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:[urlArray objectAtIndex:selectedTag]]];
                request.delegate = self;
                [request setDownloadProgressDelegate:currentProgress];
                [request setShowAccurateProgress:YES];
                [request shouldContinueWhenAppEntersBackground];
                [request allowResumeForFileDownloads];
                [request startAsynchronous];                    
            }
        }
    }
}

- (void)request:(ASIHTTPRequest *)request didReceiveData:(NSData *)data{
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(progressUpdate:) 
                                                 name:@"ProgressNotification"
                                               object:nil];
}

这工作正常,现在我想继续这个过程,即使我导航到另一个viewcontroller,也没有暂停或取消当前下载UIProgressView进度。

请帮忙。

1 个答案:

答案 0 :(得分:2)

只要在UIViewController中保存对ASIHTTPRequest的引用,它就可以工作。

其次,您必须确保未释放整个UIViewController,例如从UINavigationControllers弹出非根视图控制器。

此外,在相关说明中,请参阅页面顶部的here,ASIHTTPRequest不再被开发。

修改

我用一个带有两个标签的简单应用程序对此进行了测试,其中第一个UIViewController在加载时立即开始下载。由于ASIHTTPRequest在其自己的线程内异步运行,因此它会不断更新进度条,无论它是否在视图中。当我切换到第二个选项卡并在几秒钟后返回时,进度条已经提前。

// FirstViewController.h
#import <UIKit/UIKit.h>
#import "ASIHTTPRequest.h"

@interface FirstViewController : UIViewController {
   IBOutlet UIProgressView *progressView;
   ASIHTTPRequest *request;
}

@property (nonatomic,retain) ASIHTTPRequest *request;

@end



// FirstViewController.m
#import "FirstViewController.h"

@interface FirstViewController ()

@end

@implementation FirstViewController
@synthesize request;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = NSLocalizedString(@"First", @"First");
        self.tabBarItem.image = [UIImage imageNamed:@"first"];
        self.request = nil;
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    if (request==nil) {
        request=[[ASIHTTPRequest alloc] initWithURL:[NSURL URLWithString:@"http://largefile.zip"]];
        [request setDownloadProgressDelegate:progressView];
        [request setShowAccurateProgress:YES];
        [request shouldContinueWhenAppEntersBackground];
        [request allowResumeForFileDownloads];
        [request startAsynchronous];
    }


}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
        return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
    } else {
        return YES;
    }
}

- (void) dealloc {
    if (request!=nil) {
        [request clearDelegatesAndCancel];
        [request release];
    }
}

@end

如上所述,另一种方法是将下载数据的功能放入单例中。作为ASIHTTPRequest代理的单例将通过实现

通知您的UIViewController有关自定义通知的下载进度
- (void)request:(ASIHTTPRequest *)request didReceiveData:(NSData *)data;

并致电

     [[NSNotificationCenter defaultCenter] postNotificationName:@"MyCustomNotification" object:self];

同样,由于下载在自己的线程中运行,因此即使UIViewController不在视图中,也会收到通知。 您的UIViewController必须让NSNotificationCenter知道它想要通过调用

来接收通知
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                selector:@selector(progressUpdate:) 
                                                 name:@"MyCustomNotification"
                                               object:nil];

作为最后一点,请不要忘记致电

[[NSNotificationCenter defaultCenter] removeObserver:self];

当你解除你的UIViewController时。