0

我想使用执行多个下载过程,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进度。

请帮忙。

4

1 回答 1

2

只要您在 UIViewController 中保存对 ASIHTTPRequest 的引用,它就会起作用。

其次,你必须确保你的整个 UIViewController 没有被释放,例如从 UINavigationControllers 弹出非根视图控制器。

此外,在相关说明中,请页面顶部查看 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 时。

于 2012-04-27T13:36:24.040 回答