我使用ASIHTTPRequest來做類似的操作。通過下面的代碼更改downloadAllIcons方法來滿足您的要求,
[NSThread detachNewThreadSelector:@selector(downloadAllIcons:) toTarget:self withObject:xmlData];
-(void) downloadAllIcons:(NSData *)_xmlData
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSArray *IconList= PerformXMLXPathQuery(_xmlData,@"//icon");
[[NSUserDefaults standardUserDefaults] setObject:IconList forKey:@"IconList"];
for (int i=0; i<[IconList count]; i++) {
if ([[NSUserDefaults standardUserDefaults] objectForKey:[[IconList objectAtIndex:i] objectForKey:@"nodeContent"]]==nil) {
NSData * responseData=[self downloadProccessedImage:[[IconList objectAtIndex:i] objectForKey:@"nodeContent"]];
if(responseData)
[[NSUserDefaults standardUserDefaults] setObject:responseData forKey:[[IconList objectAtIndex:i] objectForKey:@"nodeContent"]];
//NSLog(@"%@",[[IconList objectAtIndex:i] objectForKey:@"nodeContent"]);
}
}
[pool release];
}
-(id) downloadProccessedImage:(NSString *)_URL
{
NSData *response=nil;
NSURL *url = [NSURL URLWithString:_URL];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setTimeOutSeconds:60];
[ASIHTTPRequest setShouldThrottleBandwidthForWWAN:YES];
[request startSynchronous];
NSError *error = [request error];
if (!error)
{
response = [request responseData];
}
return response;
}
嗨JFoulkes,我卡在障礙與多個NSURLConnections無關。相反,它是關於在單獨的NSThread中管理連接。正如我上面提到的,請求按順序依次進行。我搜索了它,發現了類似NSOperation的東西,但它並沒有給我準確的想法做什麼? – 2011-03-13 15:33:26