アプリケーションのダウンロード速度を上げようとしています。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);
}