あなたのいずれかがコードスニペット、複数の処理に関するチュートリアルの例を投稿できますか
ココア タッチ フレームワークを使用して同じ viewController から NSURLConnections....
今後ともよろしくお願いいたします.....
あなたのいずれかがコードスニペット、複数の処理に関するチュートリアルの例を投稿できますか
ココア タッチ フレームワークを使用して同じ viewController から NSURLConnections....
今後ともよろしくお願いいたします.....
特定の NSURLConnection がその結果を保存する必要がある NSMutableData のインスタンスを追跡する NSMutableDictionary を使用して、複数の NSUrlConnections を処理しました。
クラスの最初に、次のように定義します。
NSMutableDictionary *dataDictionary;
次に、私の loadData メソッドには、次のものがあります。
// Create the request
NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:currentCam]
cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
timeoutInterval:30];
// create the connection with the request
// and start loading the data
NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
if (theConnection) {
NSMutableData *receivedData = receivedData = [[NSMutableData alloc] init];
//keep track of this connection by adding it to subViewDictionary and dataDictionary with appropriate objects.
[dataDictionary setObject:receivedData forKey:[theConnection description]];
}
else {
NSLog(@"ERROR DOWNLOADING WITH NSURLCONNECTION");
}
[theConnection release];
[theConnection description] をキーとして使用し、MSMutableData のインスタンスをディクショナリのオブジェクトとして使用しているため、後でどのインスタンスが特定の接続に対応しているかを調べることができます。これを行わないと、データが破損する問題が発生する可能性があります (複数の接続がすべて同じ変数にデータを保存する可能性があります)。
次に、次の NSURlConnection デリゲート メソッドを定義します。
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSMutableData *)data
{
//look up in dictionary to find out which recievedData instance to use.
NSMutableData *theReceivedData = [dataDictionary objectForKey:[connection description]];
// Append the new data to receivedData.
[theReceivedData appendData:data];
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
//Lookup in the dictionary to find the correct instance of recievedData to put image into.
NSMutableData *theReceivedData = [dataDictionary objectForKey:[connection description]];
[theReceivedData setLength:0];
[activityIndicator stopAnimating];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
// inform the user that there was an error
NSLog(@"Connection failed! Error - localizedDescription:%@ NSURLErrorFailingURLStringErrorKey:%@",
[error localizedDescription],
[[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]);
NSMutableData *theReceivedData = [dataDictionary objectForKey:[connection description]];
[theReceivedData release];
//remove keys for this connection since it did not load.
[dataDictionary removeObjectForKey:[connection description]];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
//look up correct instance of recievedData in teh dictionary for this connection
NSMutableData *theReceivedData = [dataDictionary objectForKey:[connection description]];
NSLog(@"Succeeded! Received %d bytes of data",[theReceivedData length]);
//---do stuff with data here//
[theReceivedData release];
}
単一接続用のNSURlConnection hereに関する優れたチュートリアルがあります。私のコードは、各 NSUrlConnection と各 NSMutableData インスタンスを追跡するために NSMutableDictionary を追加したものに基づいています。
それが理にかなっており、役立つことを願っています!
答えはこのリンクを参照してください。nsurlconnection を asihttp でラップするので、作業がずっと楽になります。