首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >多个NSURLConnection和NSRunLoop

多个NSURLConnection和NSRunLoop
EN

Stack Overflow用户
提问于 2010-08-17 19:05:21
回答 1查看 2.2K关注 0票数 1

我正在尝试加快我的应用程序的下载速度。我使用异步NSURLConnection从服务器下载内容,它在一个连接上工作得很好。

我使用这篇文章中的代码来实现多个委托对象。Multiple NSURLConnection delegates in Objective-C

当我创建两个NSURLConnection对象时,每个对象都试图下载不同的文件。回调didReceiveData例程被调用,但它只接收第一个NSURLConnection对象的数据,直到第一个连接完成,然后它才开始从第二个NSURLConnection接收数据。我想让这两个连接同时接收数据,我该怎么做?这是我当前的代码。

代码语言:javascript
复制
-(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对象中,我实现了这些例程

代码语言:javascript
复制
- (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);
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2010-08-17 20:35:27

你的代码看起来没问题。我有一个类似的设置,它成功地工作了(尽管似乎有四个并发连接的限制)。

你的代码和我的代码之间的主要区别是你使用FTP,而我使用HTTP。为什么你不尝试一下HTTP连接,看看你是否在iPhone上遇到了FTP连接的限制?

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3501694

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档