【发布时间】: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