我正在尝试加快我的应用程序的下载速度。我使用异步NSURLConnection从服务器下载内容,它在一个连接上工作得很好。
我使用这篇文章中的代码来实现多个委托对象。Multiple NSURLConnection delegates in Objective-C
当我创建两个NSURLConnection对象时,每个对象都试图下载不同的文件。回调didReceiveData例程被调用,但它只接收第一个NSURLConnection对象的数据,直到第一个连接完成,然后它才开始从第二个NSURLConnection接收数据。我想让这两个连接同时接收数据,我该怎么做?这是我当前的代码。
-(IBAction) startDownloadClicked :(id) sender
{
while (bDownloading)
{
int nCurrentCon = 0;
while (nCurrentCon < 2)
{
[self downloadAFile:[filenameArray objectAtIndex:nCurrentCon]];
nCurrentCon++;
}
[[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.5]];
}
}
- (void) downloadAFile: (NSString*) filename
{
NSString* urlstr = @"ftp://myftpusername:password@hostname";
NSURLRequest* myreq = [NSURLRequest requestWithURL:[NSURL URLWithString:urlstr]];
DownloadDelegate* dd = [[DownloadDelegate alloc] init]; //create delegate object
MyURLConnection* myConnection = [[MyURLConnection alloc] initWithRequest:myreq delegate:dd
startImmediately:YES];
}然后在我的Delegate对象中,我实现了这些例程
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
[receiveBuffer setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
NSLog(@"receiving data for %@", targetFileName); //the file name were set when this delegate object is initialized.
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
NSLog(@"Download Failed with Error - %@ %@",
[error localizedDescription],
[[error userInfo] objectForKey:NSErrorFailingURLStringKey]);
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSLog(@"File %@ - downloaded.", targetFileName);
}发布于 2010-08-17 20:35:27
你的代码看起来没问题。我有一个类似的设置,它成功地工作了(尽管似乎有四个并发连接的限制)。
你的代码和我的代码之间的主要区别是你使用FTP,而我使用HTTP。为什么你不尝试一下HTTP连接,看看你是否在iPhone上遇到了FTP连接的限制?
https://stackoverflow.com/questions/3501694
复制相似问题