后续的NSURLConnections不会触发委托方法

问题描述:

我已经查看了所有问题的答案,并且看到了很多类似的问题,但解决方案似乎与我的情况无关。我对iOS开发非常陌生,所以不胜感激。后续的NSURLConnections不会触发委托方法

目前的应用程序正在寻找基于他们的网站生成的XML提要显示城市中发生的事件。我将事件存储在plist中,并在应用程序加载时明确检查新事件。目前的过程如下:

  • 应用程序启动并使NSURLConnection检查服务器的时间戳并下载它。
  • 检查新下载的时间戳与本地时间戳,看看它是否更大。如果是,它会重写包含plist的文件并触发一个需要新事件的通知。

这是第二个NSURLConnection似乎没有触发的地方。回调方法本身触发,但它似乎不像委托方法之后触发。

奇怪的是,如果我同时调用两个连接而不是另一个连接,他们都会触发并调用委托方法。

下面是一些类有一些为了清楚起见移除胆量的代码:

应用程序委托:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {  

// Set up analytics account 

// Set the tab bar controller as the window's root view controller and display. 
self.window.rootViewController = self.tabBarController; 
[self.window makeKeyAndVisible]; 

//Set up listeners for event handling 
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(timeStampResponse:) name:@"TimeStampResponse" object:nil];  

//Set app loading screen 

//Create new OperationQueue, set ActivityIndicator and run TimeStamp Check 
parseQueue = [NSOperationQueue new];  
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES; 
[self timeStampCheck]; 
return YES; 
} 
-(void)timeStampCheck { 
NSLog(@"Time Stamp Check, Creating Connection"); 
NSURLRequest *timeStampURLRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:kTimeStampURL]]; 
self.timeStampConnection = [[NSURLConnectionWithTag alloc] initWithRequest:timeStampURLRequest delegate:self startImmediately:YES tag:1]; 
} 

-(void)newEventsNeeded { 
NSLog(@"Event File Doesn't Exists or New File Needed, Creating Connection"); 
NSURLRequest *feedURLRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:kEventFeedURL]]; 
self.eventsFeedConnection = [[NSURLConnectionWithTag alloc] initWithRequest:feedURLRequest delegate:self startImmediately:YES tag:0]; 
} 

//Callback from DateStampParser 
-(void)timeStampResponse:(NSNotification *)notif { 
NSString *response = [[notif userInfo] objectForKey:@"TimeStampResponse"]; 
if ([response isEqualToString:@"NewEvents"]) { 
    [self newEventsNeeded]; 
    self.appLoadingViewController.loadingLabel.text = @"New Events Found..."; 
} else if ([response isEqualToString:@"NoNewEvents"]){ 
    [self allEventsLoaded]; 
} 
} 

而NSURLConnection的委托方法

-(void)connection:(NSURLConnectionWithTag *)connection didFailWithError:(NSError *)error { 
if (connection.tag == 0) { 
    NSLog(@"Connection failed on Event Feed"); 
} else if (connection.tag == 1) { 
    NSLog(@"Connection failed on Time Stamp Check"); 
} 
} 

-(void)connection:(NSURLConnectionWithTag *)connection didReceiveResponse:(NSURLResponse *)response{ 
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response; 
NSLog(@"connection:didRecieveResponse:NSHTTPURLResponse"); 
if ((([httpResponse statusCode]/100) == 2)) { 
    //Handling for eventsFeedConnection 
    if (connection.tag == 0) { 

     NSLog(@"Connection tag = 0"); 
     self.eventsData = [NSMutableData data]; 

    } else if (connection.tag == 1) { 

     NSLog(@"Connection tag = 1"); 
     //NSLog(@"Timestamp connection received response"); 
     self.timeStampData = [NSMutableData data]; 

    } 

} else { 
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Network Error" message:@"No Response from the Server" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil]; 
    [alert show]; 
    [alert release]; 
} 
} 

-(void)connection:(NSURLConnectionWithTag *)connection didReceiveData:(NSData *)data{ 
//Handling for eventsFeedConnection 
if (connection.tag == 0) { 

    self.appLoadingViewController.loadingLabel.text = @"Downloading Events..."; 
    [eventsData appendData:data]; 

} else if (connection.tag == 1) { 

    self.appLoadingViewController.loadingLabel.text [email protected]"Checking for New Events..."; 
    [timeStampData appendData:data]; 

} 
} 

-(void)connectionDidFinishLoading:(NSURLConnectionWithTag *)connection{ 
//Handling for eventsFeedConnection 
if (connection.tag == 0) { 

    NSLog(@"EventFeed Connection Finished"); 
    self.eventsFeedConnection = nil; 
    [eventsFeedConnection release]; 
    ParserOperation *parser = [[ParserOperation alloc] initWithData:self.eventsData]; 
    [parseQueue addOperation:parser]; 
    [parser release]; 
    self.eventsData = nil; 
    //[eventCategoryListViewController reenableRefreshButton]; 

} else if (connection.tag ==1){ 

    NSLog(@"TimeStamp Connection Finished"); 
    self.timeStampConnection = nil; 
    [timeStampConnection release]; 
    DateStampParser *parser = [[DateStampParser alloc] initWithData:self.timeStampData]; 
    [parseQueue addOperation:parser]; 
    [parser release]; 
    self.timeStampData = nil; 

} 

} 

不知道这个代码是否显而易见,或者不是w问题是,但如果需要任何额外的代码/澄清我想要完成什么,请让我知道。

感谢

编辑为清楚起见 只是为了澄清,在从NSLog的火灾newEventsNeeded,只是委托方法不被解雇。

+0

我无法看到您触发通知的位置。它是否在你的问题中没有显示的代码中? – Codo

+0

notif位于DateStampParser操作的末尾。它会触发由timeStampResponse收到的notif。 – Chris

+0

我知道这是一个真正的旧帖子,但我只想提到我最终修复它的方式,只是通过使用ASI HTTP库来规避真正的问题。 – Chris

确保启动NSURLConnection的线程有一个runloop。将NSURLConnection的异步回调分派到调用线程runloop上。如果调用线程(在这种情况下,它似乎是一个NSOperation队列)没有runloop,或者它没有以默认运行循环模式运行,您将看不到任何委托回调。您的第一个电话[self timeStampCheck]在主线程中,所以这很好。以后的调用来自操作队列,可能没有runloop。

the documentation相关片段:

代表

用于连接的委托对象。随着加载的进行,代表将收到 委托消息。到代理 的消息将在调用此方法的线程上发送。为使连接 正常工作,调用线程的运行循环必须在 的默认运行循环模式下运行。