UIViewController で ASIHTTPRequest への参照を保持している限り、機能します。
次に、UIViewController 全体が割り当て解除されていないことを確認する必要があります。たとえば、非ルート ビュー コントローラーが UINavigationControllers からポップされた場合に発生します。
また、関連する注意事項として、ASIHTTPRequest が現在積極的に開発されていないページの上部にあるこちらを参照してください。
編集:
2 つのタブを持つ単純なアプリでこれをテストしました。最初の UIViewController は、読み込まれるとすぐにダウンロードを開始します。ASIHTTPRequest は独自のスレッド内で非同期に実行されるため、表示されているかどうかに関係なく、進行状況バーを更新し続けます。2 番目のタブに切り替えて数秒後に戻ると、プログレス バーが進んでいます。
// 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 の割り当てを解除するとき。