我的计时器没有停止我的解析

发布于 2024-09-30 00:03:43 字数 1644 浏览 4 评论 0原文

我想设置一个超时,以防查找位置、发送相关 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];

}

感谢您的帮助。

I want to put in a timeout in case it takes too long to find my location, send out the relevant url, and parse the xml. It worked when I used performSelector:withObject:afterDelay in the locationManager (just to test getting the xml), but when I put similar code around my parser it doesn't actually abort the parsing. I am testing this by dropping the delay to 0.01.

My problem is: even with the delay set to 0.01, it still waits for all the parsing to complete first, and only then does it put up the alertView that is coded in the parsingDidTimeout method.

I did try this with a timer, and that wasn't working as well as performSelector: does in the other parts of my code. Either way, it doesn't put up the alertView, and stop the parsing, until after the parsing has finished, no matter how long that takes.

I create a url which requires a radius. First I try a small radius, but if I don't get the data I need, I expand the radius and send the url again and parse again. Here is part of my StartParsing method.

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

}

Thank you for your help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

巷子口的你 2024-10-07 00:03:43

调用performSelector:withObject:afterDelay:您要求运行循环稍后调用选择器。但是 [xmlParser parse] 会阻止运行循环,因此它没有机会调用您的选择器。

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

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

Calling performSelector:withObject:afterDelay: you ask the run loop to call the selector later. But [xmlParser parse] blocks the run loop, so it doesn't have a chance to call you selector.

abortParsing is designed to be called inside parsers' delegate methods.

The workaround can be to parse in a separate thread.

任谁 2024-10-07 00:03:43

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

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

Found it -- just extra ":" in my performSelector:@selector(parsingDidTimeout:)!
I thought it was something fancy having to do with the second thread. Just syntax.

Thanks for explaining about the parse blocking the run loop. I was hoping not to need another thread, but your suggestion fixed my problem. Thanks.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文