NSURLConnection挂在二次电话

时间:2011-01-15 21:37:29

标签: ios nsurlconnection

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

我有另一个类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];
}

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];
}

1 个答案:

答案 0 :(得分:0)

我能够通过删除用于NSXMLParser的WeatherUpdater中的线程来解决这个问题。我从Apple的SiesmicXML示例应用程序中“解压”了该代码。