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

我已经找遍了这个问题的答案,并已经看到了很多类似的问题,但解决scheme似乎没有涉及到我的情况。 我很新的iOS开发,所以任何帮助,将不胜感激。

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

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

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

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

下面是一些类的代码,为了清楚起见删除了一些内容:

申请代表:

- (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 =@"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; } } 

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

谢谢

编辑清晰只是为了澄清,从newEventsNeeded的NSLog需要触发,但只是委托方法不会被解雇。

确保启动NSURLConnection的线程有一个runloop。 NSURLConnection的asynchronouscallback被调度到调用线程runloop上。 如果调用线程(在这种情况下,它似乎是一个NSOperation队列)没有runloop,或者如果它不运行在默认运行循环模式,您将看不到任何代表callback。 你第一次打电话给[self timeStampCheck]是在主线程,所以这很好。 之后的调用来自操作队列,可能没有runloop。

文档中的相关片段:

代表

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