NSURLConnection挂在第二个电话

我正在构build一个iPhone应用程序,用于汇总来自多个不同数据源的数据,并将它们一起显示在一个表中。 对于每个数据源,我创build了一个类( WeatherProviderTwitterProvider ),用于处理连接到数据源,下载数据并将其存储在一个对象中。

我有另一个类ConnectionManager ,它实例化和调用两个提供程序类中的每一个,并将结果合并到一个单独的NSArray显示在表中。

当我运行程序调用WeatherProvider或只是TwitterProvider,它完美的作品。 我可以一次又一次地调用这些对象,使它们保持最新,而不会出现问题。 我也可以调用TwitterProvider,然后WeatherProvider没有问题。

但是,如果我调用WeatherProvider然后TwitterProvider,TwitterProvider就像我打电话一样挂起: [[NSURLConnection alloc] initWithRequest:request delegate:self];

其他点: – 当我打电话给WeatherProvider和TwitterProvider,TwitterProvider仍然挂起之间似乎没有多久。 – 在WeatherProvider中,我使用NSXMLParser和NSAutoreleasePool来parsingWeatherProvider的输出。 – 当应用程序启动时,ConnectionManager创buildWeatherProvider和TwitterProvider的实例,并在用户请求数据刷新时重用这些实例。 – 我运行与活动监视器连接的应用程序,它validation应用程序基本上是挂起来的。 没有CPU使用率,或额外的内存分配或networking活动似乎正在发生。

在一对夫妇文件中有一堆代码,所以我试图在这里包括相关的位(据我所知!)。 我非常感谢您提供的任何帮助,即使这只是其他debugging方法。

WeatherProvider

 -(void)getCurrentWeather: (NSString*)lat lon:(NSString*)lon lastUpdate:(double)lastUpdate { double now = [[NSDate date] timeIntervalSince1970]; NSString *noaaApiUrl; // don't update if current forecast is < than 1 hour old if(now - lastUpdate < 3600) { [[self delegate] weatherUpdaterComplete:1]; return; } // if we already have a forecast, delete and refill it. if(forecast) { [forecast release]; forecast = [[WeatherForecast alloc] init]; } forecast.clickThroughUrl = [NSString stringWithFormat:@"http://forecast.weather.gov/MapClick.php?lat=%@&lon=%@", lat, lon]; noaaApiUrl = [NSString stringWithFormat:@"http://www.weather.gov/forecasts/xml/sample_products/browser_interface/ndfdBrowserClientByDay.php?lat=%@&lon=%@&format=24+hourly", lat, lon]; NSURLRequest *noaaUrlRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:noaaApiUrl]]; [[NSURLConnection alloc] initWithRequest:noaaUrlRequest delegate:self]; } #pragma mark - #pragma mark NSURLConnection delegate methods - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { self.noaaData = [NSMutableData data]; } - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { [noaaData appendData:data]; } - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { self.noaaConnection = nil; [[self delegate] weatherUpdaterError:error]; [connection release]; } - (void)connectionDidFinishLoading:(NSURLConnection *)connection { // Spawn a thread to fetch the data so UI isn't blocked while we parse // the data. IMPORTANT - don't access UIKit objects on 2ndary threads [NSThread detachNewThreadSelector:@selector(parseNoaaData:) toTarget:self withObject:noaaData]; // the noaaData will be retailed by the thread until parseNoaaData: has finished executing // so, we'll no longer need a reference to it in the main thread. self.noaaData = nil; [connection release]; } #pragma mark - #pragma mark NSXMLParser delegate methods - (void)parseNoaaData:(NSData *)data { NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; NSXMLParser *parser = [[NSXMLParser alloc] initWithData:data]; [parser setDelegate:self]; [parser parse]; [parser release]; [pool release]; } 

TwitterProvider

 -(void)getLocationTimeline:(NSString*)lat lon:(NSString*)lon lastUpdate:(double)lastUpdate refreshUrl:(NSString*)newUrl { NSString *updateURL; if(tweets.count > 1) [tweets removeAllObjects]; updateURL = [NSString stringWithFormat:@"http://search.twitter.com/search.json?geocode=%@,%@,1mi&rpp=%@&page=1", lat, lon, TWITTER_LOCAL_UPDATES_URL_RESULTS]; _serviceResponse = [[NSMutableData data] retain]; NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:updateURL]]; [[NSURLConnection alloc] initWithRequest:request delegate:self]; } #pragma mark - #pragma mark NSURLConnection delegate methods - (void)connectionDidFinishLoading:(NSURLConnection *)connection { NSString *responseString = [[NSString alloc] initWithData:_serviceResponse encoding:NSUTF8StringEncoding]; // parse tweets [responseString release]; [_serviceResponse release]; [connection release]; // tell our delegate that we're done! [[self delegate] twitterUpdaterComplete:tweets.count]; } - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { [_serviceResponse setLength:0]; } - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { [_serviceResponse appendData:data]; } - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { [connection release]; [[self delegate] twitterUpdaterError:error]; } 

我能够通过删除正在用于NSXMLParser的WeatherUpdater中的线程来解决此问题。 我曾经从苹果的SiesmicXML示例应用程序中“发现”这些代码。