我在使用NSURLDownload时遇到了一点小问题。基本上,我使用下面的代码两次下载两个不同的文件,并更改了变量名。
NSURLRequest *requestUserfile = [NSURLRequest requestWithURL:[NSURL URLWithString:requestAgent_url] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
NSURLDownload *downloadUserFile = [[NSURLDownload alloc] initWithRequest:requestUserfile delegate:self];
NSString *uf_tpath = [ [ [NSBundle mainBundle] bundlePath ] stringByAppendingPathComponent:@"userfile"];
if (downloadUserFile) {
[downloadUserFile setDestination:uf_tpath allowOverwrite:YES];
} else {
NSLog("ERROR: Problem while downloading user file [1].");
exit(0);
}但我有一个问题,根据苹果的类引用,这段代码是异步的,当下载完成时,它会调用- (void)downloadDidFinish:(NSURLDownload *)download。
我的程序需要在开始任何处理之前将这两个文件下载到一个文件夹中,那么我如何知道这两个文件何时下载?它将在第一个任务完成时调用:S
我的第一个想法是将第二个下载的文件放入第一个文件的downloadDidFinish中……但是之后我会遇到一个无限循环,我会有两个同名的方法:S
PS:我是Obj-C和Xcode的新手,我曾经使用QT上的信号和插槽来做这件事,但在那里我可以定义两个插槽不同的插槽,并在正确的时间连接它们。
谢谢。
发布于 2011-09-15 01:33:57
我最终在(void)downloadDidFinish:(NSURLDownload *)download上应用了一条if语句来评估磁盘上已经存在的文件,然后下载另一个文件。
喜欢
NSString *file1_path = [ [ [NSBundle mainBundle] bundlePath ] stringByAppendingPathComponent:@"file1"];
NSString *file2_path = [ [ [NSBundle mainBundle] bundlePath ] stringByAppendingPathComponent:@"file2"];
BOOL f1_is_downdl = [[NSFileManager defaultManager] fileExistsAtPath:file1_path];
BOOL f2_is_downdl = [[NSFileManager defaultManager] fileExistsAtPath:file2_path];
if ( f1_is_downdl && !(f2_is_downdl) ) { // File 1 is downloaded and the second is not...
[download release];
// Now download the file 2!
NSString *requestFile2_url = [NSString stringWithFormat:@"http://host.org/software/%@-ag", req_id];
NSURLRequest *requestFile2 = [NSURLRequest requestWithURL:[NSURL URLWithString:requestFile2_url] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
NSURLDownload *downloadFile2 = [[NSURLDownload alloc] initWithRequest:requestFile2 delegate:self];
if (downloadFile2) {
[downloadAgent setDestination:file2_path allowOverwrite:YES];
} else {
NSLog(@"ERROR: Problem while downloading user file [2].");
exit(0);
}
} else if (f1_is_downdl && f2_is_downdl) { // Everything is downloaded!
// Do something else.
}好吧..。我知道这可能是世界上最糟糕的方式,但是...我就是这么想出来的。
无论如何,谢谢你的信息,总有一天(当我用更多的Obj-C知识重写这篇文章时),它可能会对我有用。
https://stackoverflow.com/questions/7403904
复制相似问题