多个 NSURLConnection & NS运行循环

发布于 2024-09-14 08:12:02 字数 2090 浏览 1 评论 0原文

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

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

当我创建 2 个 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];

}

然后在我的委托对象中,我实现了这些例程

- (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);
}

I am trying to speed up my application download speed. I used Asynchronous NSURLConnection to download contents from the server, it was working fine with one connection.

I use the code from this post to implement multiple delegate objects. Multiple NSURLConnection delegates in Objective-C

When I created 2 NSURLConnection objects, each one is trying to download different files.
The callback didReceiveData routine was called but the it only received data of the first NSURLConnection object until the first connection was done then it started to receive the data from the second NSURLConnection. I want these two connections to receive data at the same time,what should I do? Here is my current code.

-(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];

}

Then in my Delegate Object, I implemented these routines

- (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);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦年海沫深 2024-09-21 08:12:02

你的代码看起来没问题。我有一个类似的设置,可以成功运行(尽管似乎有四个并发连接的限制)。

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

Your code looks okay. I have a similar setup that works successfully (although there seems to be a limit of four concurrent conections).

The main difference between your and my code is that you use FTP while I use HTTP. Why don't you try it with HTTP connections just to see whether you have run into a restriction of FTP connections on the iPhone?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文