views:

210

answers:

1

Hi,

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. http://stackoverflow.com/questions/203294/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);
}
A: 

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?

Codo
Yes, it works now. I changed from FTP to HTTP now both connections can receive data at the same time.Thank you.
Aing