1

アプリケーションのダウンロード速度を上げようとしています。Asynchronous NSURLConnection を使用してサーバーからコンテンツをダウンロードしましたが、1 つの接続で正常に動作していました。

この投稿のコードを使用して、複数のデリゲート オブジェクトを実装します。Objective-C の複数の NSURLConnection デリゲート

2 つの NSURLConnection オブジェクトを作成したとき、それぞれが異なるファイルをダウンロードしようとしています。コールバック didReceiveData ルーチンが呼び出されましたが、最初の接続が完了するまで最初の NSURLConnection オブジェクトのデータのみを受信し、その後 2 番目の NSURLConnection からデータの受信を開始しました。これら 2 つの接続で同時にデータを受信したいのですが、どうすればよいですか? これが私の現在のコードです。

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

1 に答える 1

1

あなたのコードは問題ないようです。私は正常に動作する同様のセットアップを持っています (ただし、同時接続は 4 つに制限されているようです)。

あなたのコードと私のコードの主な違いは、私が HTTP を使用しているのに対し、あなたは FTP を使用していることです。iPhone で FTP 接続の制限に遭遇したかどうかを確認するためだけに、HTTP 接続で試してみませんか?

于 2010-08-17T12:35:27.203 に答える