2011-09-16 13 views
1

を管理するので、最初に私のセットアップの2つの要求:次に私は2つの異なったKMLファイルから2つの非同期リクエストをしたい2 NSURLConnection

AResponseData = [[NSMutableData alloc] init]; 
BResponseData = [[NSMutableData alloc] init]; 

NSString *server1URL = [NSString stringWithFormat:...]; 
NSMutableURLRequest *firstRequest =[NSMutableURLRequest requestWithURL:[NSURL URLWithString:server1URL]]; 
[firstRequest setHTTPMethod:@"GET"]; 
NSURLConnection *AConnection = [NSURLConnection connectionWithRequest:firstRequest delegate:self]; 

NSString *server2URL = [NSString stringWithFormat:...]; 
NSMutableURLRequest *secondRequest =[NSMutableURLRequest requestWithURL:[NSURL URLWithString:server2URL]]; 
[secondRequest setHTTPMethod:@"GET"]; 
NSURLConnection *BConnection = [NSURLConnection connectionWithRequest:secondRequest delegate:self]; 

は、それから私は、私が使用するNSMutableDataを初期化、私はthis記事を参照して、このでした:

connectionToInfoMapping = CFDictionaryCreateMutable(kCFAllocatorDefault, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks); 
CFDictionaryAddValue(connectionToInfoMapping, AConnection, [NSMutableDictionary dictionaryWithObject:AResponseData forKey:@"receivedData"]); 
CFDictionaryAddValue(connectionToInfoMapping, BConnection, [NSMutableDictionary dictionaryWithObject:BResponseData forKey:@"receivedData"]); 

[OK]を、そしてそこにいる代表者:

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 
{ 
    const NSMutableDictionary *connectionInfo = CFDictionaryGetValue(connectionToInfoMapping, connection); 
    [[connectionInfo objectForKey:@"receivedData"] appendData:data]; 
} 

だからこれで、私は、データが接続に一致する正しいNSMutableDataに追加取得することができます。

- (void)connectionDidFinishLoading:(NSURLConnection *)connectionで、私は「これが終わったら、これで終わり、これをやりなさい」と欲しいと思います。私の質問はどうしたらいいですか?

+0

私はNSURLConnectionをサブクラス化し、キーまたはで各要求を識別するためのタグのような項目を追加し、あなたがリクエストを初期化するinitメソッドをオーバーライドすることを利用してお勧めします。これは私が現在複数の非同期接続が必要な私のアプリでやっていることです。私は[this](http://blog.emmerinc.be/index.php/2009/03/02/custom-nsurlconnection-class-with-tag/)と[this](http://blog.emmerinc。/index.php/2009/03/15/multiple-async-nsurlconnections-example /)を使用して、アプリケーションの問題を解決してください。 – Bourne

答えて

2
- (void)connectionDidFinishLoading:(NSURLConnection *)connection{ 
    if([connection isEqual: AConnection]){ 
     // do connection A stuff 
    } 
    else if([connection isEqual: BConnection]){ 
     // do connection B stuff 
    } 
} 
+0

このメソッドを呼び出してシリアルに呼び出しを試みたときに問題が発生しました。最初の接続が完了して2番目の接続が確立されました。 2回目の接続が復帰したとき、私は非常に奇妙な動作をしました。アラートは表示されず、セグは実行されませんでした。奇妙なことに、私は何の誤りもなかった。 –

-1

各接続にタグを割り当て、connectionDidFinishLoadingでif/elseまたはスイッチを使用してタグを確認する方法はありますか?

+0

接続に「タグ」を割り当てるにはどうすればよいですか? 'NSURLConnection'をサブクラス化する必要はありますか?ありがとう。 –

+0

はいNSURLConnectionにタグを追加するシンプルなカスタムクラスです。それはトリックを行う必要があります。編集:その目的のために、カスタムタグを扱う必要がなく、connectionDidFinishLoadingでURL自体をチェックする必要がないように、カスタムクラスに実際のURLを格納することもできます。 – bizsytes

0

sendSynchronousRequest:要求でGCDを使用すると、それらはバックグラウンドで実行されます。

例:

dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); 

dispatch_async(queue, ^{ 
    NSURLRequest *request = [NSURLRequest requestWithURL:url1]; 
    NSURLResponse *response; 
    NSError *error; 
    NSData *data1 = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]; 
    // do something with the data 
}); 

dispatch_async(queue, ^{ 
    NSURLRequest *request = [NSURLRequest requestWithURL:url2]; 
    NSURLResponse *response; 
    NSError *error; 
    NSData *data2 = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]; 
    // do something with the data 
}); 
関連する問題