【问题标题】:My timer isn't stopping my parsing我的计时器没有停止我的解析
【发布时间】:2010-10-31 16:05:17
【问题描述】:

我想设置一个超时,以防找到我的位置需要很长时间,发送相关的 url,并解析 xml。当我在 locationManager 中使用 performSelector:withObject:afterDelay 时它起作用了(只是为了测试获取 xml),但是当我在解析器周围放置类似的代码时,它实际上并没有中止解析。我正在通过将延迟降低到 0.01 来测试这一点。

我的问题是:即使将延迟设置为 0.01,它仍然会先等待所有解析完成,然后才会放上 parsingDidTimeout 方法中编码的 alertView。

我确实尝试过使用计时器,但效果不如 performSelector: 在我代码的其他部分中的效果。无论哪种方式,它都不会设置 alertView 并停止解析,直到解析完成,无论需要多长时间。

我创建了一个需要半径的网址。首先我尝试一个小半径,但如果我没有得到我需要的数据,我扩大半径并再次发送 url 并再次解析。这是我的 StartParsing 方法的一部分。

 xmlParser = [[NSXMLParser alloc] initWithContentsOfURL:url];
 XMLParser *parser = [[XMLParser alloc] initXMLParser];
[xmlParser setDelegate:parser];

 if (!hadToExpandRadius){//meaning, only do this the first time I send out the url and parse
 [self performSelector:@selector(parsingDidTimeout:) withObject:nil afterDelay:0.01];
 }
 //Start parsing the XML file.
 BOOL success = [xmlParser parse];

 if(success){
 if((didNotGetTheDataYet) && (radius < 500)){
 hadToExpandRadius = YES;
 radius = radius + 35;
 [self startParsing];//do this same method, with larger radius
 }
 else {
 NSLog(@"No Errors");
 [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(parsingDidTimeout:) object:nil];}
 [parser release];
 }

 -(void)parsingDidTimeout{
       [xmlParser abortParsing];
  UIAlertView *servicesDisabledAlert = [[UIAlertView alloc] initWithTitle:@"Try Later" message:@"We need a better connection. We can get the data later." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
  [servicesDisabledAlert show];
  [servicesDisabledAlert release];
  [myActivityView stopAnimating];

}

感谢您的帮助。

【问题讨论】:

    标签: objective-c timer nsxmlparser uialertview xml-parsing


    【解决方案1】:

    调用performSelector:withObject:afterDelay: 你要求运行循环稍后调用选择器。但是[xmlParser parse] 阻塞了运行循环,所以它没有机会调用你的选择器。

    abortParsing 设计为在解析器的委托方法中调用。

    解决方法可以是在单独的线程中解析。

    【讨论】:

    • 现在我有'[self performSelector:@selector(parsingDidTimeout:) withObject:nil afterDelay:2];'然后是“[self performSelectorInBackground:@selector(backgroundParse) withObject:nil]”。然后 backgroundParse 调用 '[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(parsingDidTimeout:) object:nil];'当它成功时。现在我的问题是,当超时发生时,它调用 parsingDidTimeout,我得到一个无法识别的选择器错误。我目前有'[xmlParser abortParsing];'在 parsingDidTimeout 内,但即使这样,我也会收到错误消息。啊。怎么办?谢谢!
    • 似乎只是一个错字。在@selector(parsingDidTimeout:) 中有一个':',但是方法被声明为nullary。
    • 我正在从我的 parsingDidTimeout 方法调用 [xmlParser abortParsing]。如何从我的委托方法中调用它?那是在另一个线程上,但我需要从我的主线程调用 parsingDidTimeout 。即使弹出了 alertView,它仍然没有中止解析。
    • 您可以在超时后设置一个原子标志属性,然后在每个委托回调开始时检查它并调用 abortParsing。
    【解决方案2】:

    找到了——只是在我的 performSelector:@selector(parsingDidTimeout:) 中多了一个“:”! 我认为这与第二个线程有关。只是语法。

    感谢您解释阻塞运行循环的解析。我希望不需要另一个线程,但你的建议解决了我的问题。谢谢。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多