IOS队列异步下载
我面临一个问题,使异步下载3个文件的队列 我想当我完成下载并保存第一个文件开始下载第二个然后第三个 目前,我正在使用3 iAction下载到Documents文件夹中,它工作得很好,但要使它自动用于所有文件,却无法工作 实现此文件下载队列的最佳方法是什么? 我知道我必须对didReceiveData进行声明,但我需要帮助才能使其正常工作 这是我正在使用的代码:IOS队列异步下载,ios,xcode,download,queue,Ios,Xcode,Download,Queue,我面临一个问题,使异步下载3个文件的队列 我想当我完成下载并保存第一个文件开始下载第二个然后第三个 目前,我正在使用3 iAction下载到Documents文件夹中,它工作得很好,但要使它自动用于所有文件,却无法工作 实现此文件下载队列的最佳方法是什么? 我知道我必须对didReceiveData进行声明,但我需要帮助才能使其正常工作 这是我正在使用的代码: // Download song 1 - (IBAction)download { [self performSelector:@se
// 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];
}
在控制器中,创建三个块并将它们复制到一个数组中,该数组将用作队列。该数组需要存储为实例变量,以便以后调用控制器类中的方法可以访问它。三个块中的每一个都应该创建并执行一个NSURLConnection,该连接异步下载相应的文件。每个NSURLConnection的委托可以是您的控制器,它应该实现
-connectiondFinishLoading:
委托方法。从这个方法中,调用一个从队列中弹出第一个块并执行它的方法
然后第一次调用该方法就可以开始这个过程。显然,您需要提供一些边缘情况和错误处理,但这是基本思想