Ios 第二次呼叫时NSURL连接挂起

Ios 第二次呼叫时NSURL连接挂起,ios,nsurlconnection,Ios,Nsurlconnection,我正在构建一个iPhone应用程序,它可以聚合来自多个不同数据源的数据,并将它们显示在一个表中。对于每个数据源,我都创建了一个类(WeatherProvider,TwitterProvider),用于处理连接到数据源、下载数据并将其存储在对象中 我有另一个类ConnectionManager,它实例化并调用两个提供程序类中的每一个,并将结果合并到表中显示的单个NSArray中 当我运行程序调用WeatherProvider或TwitterProvider时,它工作得非常好。我可以一次又一次地调用

我正在构建一个iPhone应用程序,它可以聚合来自多个不同数据源的数据,并将它们显示在一个表中。对于每个数据源,我都创建了一个类(
WeatherProvider
TwitterProvider
),用于处理连接到数据源、下载数据并将其存储在对象中

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

当我运行程序调用WeatherProvider或TwitterProvider时,它工作得非常好。我可以一次又一次地调用这些对象中的每一个,使它们保持最新,而不会出现问题。我也可以打电话给TwitterProvider,然后再打给WeatherProvider,没有问题

但是,如果我先调用WeatherProvider,然后再调用TwitterProvider,TwitterProvider将在调用时挂起:
[[NSURLConnection alloc]initWithRequest:request delegate:self]

其他要点: -我调用WeatherProvider和TwitterProvider之间的时间间隔似乎并不重要,TwitterProvider仍然挂起。 -在WeatherProvider中,我使用NSXMLParser和NSAutoreleasePool来解析WeatherProvider的输出。 -ConnectionManager在应用程序启动时创建WeatherProvider和TwitterProvider的实例,并在用户请求数据刷新时重用这些实例。 -我在连接了活动监视器的情况下运行了该应用程序,它验证了该应用程序基本上处于挂起状态。没有CPU使用,或额外的内存分配或网络活动似乎正在发生

有一堆代码分布在几个文件中,因此我尝试在这里包含相关的位(据我所知!)。我非常感谢您提供的任何帮助,即使这只是调试的附加方法

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];
}
-(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中的线程来解决这个问题。我从苹果的SiemicXML示例应用程序中“掏出”了这段代码