Reputation: 1091
I want to perform a multiple download process using ASIHTTPRequest
and I have implemented with the code
Edit:
- (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];
}
this is working fine, now I want to continue the process, even if I navigate to the another viewcontroller
, without pausing or cancelling the current download with the UIProgressView
progress.
Please help.
Upvotes: 0
Views: 1759
Reputation: 362
As long as you hold a reference to your ASIHTTPRequest in your UIViewController it will work.
Secondly, you have to make sure that your entire UIViewController is not deallocated which happens for example with non-root view controllers are popped from UINavigationControllers.
Also, on a related note, please see here on top of the page that ASIHTTPRequest is not actively developed anymore.
Edit:
I've tested this with a simple app with two tabs, where the first UIViewController immediately starts a download when loaded. Since ASIHTTPRequest runs asynchronously within its own thread, it keeps updating the progress bar, regardless of whether it is in view or not. When I switch to the second tab and return after a few seconds, the progress bar has advanced.
// 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
As suggested above, another way of doing this is to put the functionality of downloading the data into a singleton. The singleton, being a delegate to ASIHTTPRequest would then for example notify your UIViewController about the download progress with custom notifications by implementing
- (void)request:(ASIHTTPRequest *)request didReceiveData:(NSData *)data;
and calling
[[NSNotificationCenter defaultCenter] postNotificationName:@"MyCustomNotification" object:self];
Again, since the download runs in its own thread, your UIViewController will be notified even if it is not in view. Your UIViewController has to let the NSNotificationCenter know that it wants to receive notifications by calling
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(progressUpdate:)
name:@"MyCustomNotification"
object:nil];
As a final note, don't forget to call
[[NSNotificationCenter defaultCenter] removeObserver:self];
when you dealloc your UIViewController.
Upvotes: 2