2011-08-24 131 views
0

我正面臨一個問題,使得3個文件的異步下載隊列。IOS隊列異步下載

當我完成下載和保存的第一文件開始下載第二個,然後第三個我想......

對於我使用3 IBAction爲下載到的文件夾,它的工作時刻完美,但爲了使所有文件自動無效。

實現此文件下載隊列的最佳方式是什麼? 我知道我必須有關於didReceiveData的聲明,但我需要幫助才能使其工作。

這是我使用的代碼:

// Download song 1 
- (IBAction)download { 

[self performSelector:@selector(downloadmusic) withObject:nil afterDelay:0.0]; 

} 


- (void)downloadmusic 
{ 
self.log = [NSMutableString string]; 
[self doLog:@"1/13"]; 

// Retrieve the URL string 
int which = [(UISegmentedControl *)self.navigationItem.titleView selectedSegmentIndex]; 
NSArray *urlArray = [NSArray arrayWithObjects: SONG1_URL, nil]; 
NSString *urlString = [urlArray objectAtIndex:which]; 

// Prepare for download 
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES]; 

// Set up the Download Helper and start download 
[DownloadHelper sharedInstance].delegate = self; 
[DownloadHelper download:urlString]; 

} 

// Download song 2 
- (void)downloadmusic2 
{ 

self.log = [NSMutableString string]; 
[self doLog:@"2/13"]; 

// Retrieve the URL string 
int which = [(UISegmentedControl *)self.navigationItem.titleView  selectedSegmentIndex]; 
NSArray *urlArray = [NSArray arrayWithObjects: SONG2_URL, nil]; 
NSString *urlString = [urlArray objectAtIndex:which]; 

// Prepare for download 
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES]; 

// Set up the Download Helper and start download 
[DownloadHelper sharedInstance].delegate = self; 
[DownloadHelper download:urlString]; 


} 


// Download song 3 
- (void)downloadmusic3 
{ 

self.log = [NSMutableString string]; 
[self doLog:@"3/13"]; 

// Retrieve the URL string 
int which = [(UISegmentedControl *)self.navigationItem.titleView selectedSegmentIndex]; 
NSArray *urlArray = [NSArray arrayWithObjects: SONG3_URL, nil]; 
NSString *urlString = [urlArray objectAtIndex:which]; 

// Prepare for download 
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES]; 

// Set up the Download Helper and start download 
[DownloadHelper sharedInstance].delegate = self; 
[DownloadHelper download:urlString]; 


} 



- (void) doLog: (NSString *) formatstring, ... 
{ 
va_list arglist; 
if (!formatstring) return; 
va_start(arglist, formatstring); 
NSString *outstring = [[[NSString alloc] initWithFormat:formatstring arguments:arglist] autorelease]; 
va_end(arglist); 
[self.log appendString:outstring]; 
[self.log appendString:@"\n"]; 
[textView setText:self.log]; 
} 

- (void) restoreGUI 
{ 
self.navigationItem.rightBarButtonItem = BARBUTTON(@"Get Data", @selector(action:)); 
if ([[NSFileManager defaultManager] fileExistsAtPath:DEST_PATH]) 
    self.navigationItem.leftBarButtonItem = BARBUTTON(@"Play", @selector(startPlayback:)); 
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO]; 
[(UISegmentedControl *)self.navigationItem.titleView setEnabled:YES]; 
[progress setHidden:YES]; 

} 

- (void) dataDownloadAtPercent: (NSNumber *) aPercent 
{ 
[progress setHidden:NO]; 
[progress setProgress:[aPercent floatValue]]; 
} 

- (void) dataDownloadFailed: (NSString *) reason 
{ 
[self restoreGUI]; 
if (reason) [self doLog:@"Download failed: %@", reason]; 
} 

- (void) didReceiveFilename: (NSString *) aName 
{ 
self.savePath = [DEST_PATH stringByAppendingString:aName]; 


} 

- (void) didReceiveData: (NSData *) theData 
{ 
if (![theData writeToFile:self.savePath atomically:YES]) 
    [self doLog:@"Error writing data to file"]; 

[theData release]; 
[self restoreGUI]; 
[self doLog:@"Download succeeded"]; 


//[self performSelector:@selector(downloadmusic2) withObject:nil afterDelay:1.0]; 


    //[self performSelector:@selector(downloadmusic3) withObject:nil afterDelay:1.0]; 


} 

回答

2

從你的控制器中,創建三個塊,並將它們複製到一個數組,這將作爲您的隊列。這個數組需要被存儲爲一個實例變量,以便稍後調用控制器類中的方法來訪問它。三個塊中的每一個都應該創建並執行一個NSURLConnection,它可以異步下載相應的文件。每個NSURLConnection的委託可以是你的控制器,它應該實現-connectionDidFinishLoading:委託方法。從這個方法中,調用一個方法,將第一個塊從隊列中彈出並執行。

然後只是第一次調用該方法來啓動該過程。顯然你需要提供一些邊緣情況和錯誤處理,但這是基本的想法。