0

我想用ASIHTTPRequest執行多個下載過程,我都用代碼如何使用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的進展。

請幫忙。

+0

如何將ASIHTTPRequest的引用保存在獨立於您的VC的全局單例中? – Till 2012-04-27 13:39:14

+0

但我也想保持progressview移動,這是否可能? – Mithuzz 2012-04-28 04:28:25

回答

2

只要你在你的UIViewController中持有對你的ASIHTTPRequest的引用,它就可以工作。

其次,你必須確保你的整個UIViewController不會被釋放,例如在UINavigationControllers中彈出非root用戶視圖控制器。

此外,在相關說明中,請參閱頁面頂部的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會再例如通過實施

- (void)request:(ASIHTTPRequest *)request didReceiveData:(NSData *)data; 

,並呼籲

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

再次通知你關於自定義通知的下載進度的UIViewController,因爲下載在自己的線程中運行即使它不在視圖中,你的UIViewController也會被通知。 UIViewController中有讓NSNotificationCenter知道,它想要通過調用

 [[NSNotificationCenter defaultCenter] addObserver:self 
               selector:@selector(progressUpdate:) 
               name:@"MyCustomNotification" 
               object:nil]; 

最後要注意的接收通知,不要忘記調用

[[NSNotificationCenter defaultCenter] removeObserver:self]; 

當你的dealloc UIViewController中。

+0

我在那個頁面看到了他的更新,但是它爲我工作。我只是害怕如何創建另一個班級保持參考,任何想法? – Mithuzz 2012-04-30 10:46:13

+0

@John,我在回答中添加了一些代碼以幫助您入門,希望這有助於您。 – zero0cool 2012-04-30 12:06:18

+0

@zeroOcool感謝您的代碼。你有沒有嘗試過這種多次下載?我如何控制多個進程?我已經更新了我的代碼,請檢查,這是正確的方式嗎? – Mithuzz 2012-05-02 07:11:49

相關問題