你们中的任何人都可以发布代码片段,关于处理多个的示例教程
使用 cocoa Touch 框架来自同一个 viewController 的 NSURLConnections....
感谢您未来的所有帮助......
你们中的任何人都可以发布代码片段,关于处理多个的示例教程
使用 cocoa Touch 框架来自同一个 viewController 的 NSURLConnections....
感谢您未来的所有帮助......
我使用 NSMutableDictionary 处理了多个 NSUrlConnections,它跟踪特定 NSURLConnection 应该将其结果保存到哪个 NSMutableData 实例。
在我的课开始时,我定义:
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 的很好的教程。我的代码基于它添加了 NSMutableDictionary 来跟踪每个 NSUrlConnection 和每个 NSMutableData 实例。
我希望这是有道理的并且是有帮助的!
请参阅此链接以获取答案。它用 asihttp 封装了 nsurlconnection,让你的生活更轻松。