我使用performSelector以不同的时间戳每隔几秒调用一次URLRequest。但是,数据处理可能需要比我定义的时间更长的时间。
[self performSelector:@selector(process) withObject:nil afterDelay:1.6];
下面的部分显示了该方法被称为
-(void)process
{
timestamp=[NSString stringWithFormat:@"%1.f",progressValue];
NSString *contour=@"&bandschema=4";
NSString *url6=[NSString stringWithFormat:@"http://contour.php? callback=contourData%@&type=json×tamp=%@%@",timestamp,timestamp,contour];
NSURL *url1=[NSURL URLWithString:url6];
__weak ASIHTTPRequest *request1 = [ASIHTTPRequest requestWithURL:url1];
[request1 setCompletionBlock:^{
responseString = [request1 responseString];
[self plotPoint:self.responseString];
}];
[request1 setFailedBlock:^{
NSError *error=[request1 error];
NSLog(@"Error: %@", error.localizedDescription);
}];
[request1 startAsynchronous];
}
这部分是分析数据的起点。
-(void)plotPoint:(NSString *)request
{
NSArray *polygonArray = [[dict objectForKey:@"data"]valueForKey:@"polygon"];
NSArray *valleyPolygonArray = [[dict objectForKey:@"valley"]valueForKey:@"polygon"];
CLLocationCoordinate2D *coords;
}
然而,有时候时间间隔不足以获得新数据,尤其是当互联网连接不好时。
你可以指导我吗?我怎么能处理这个问题?什么是最佳解决方案?答案 0 :(得分:0)
为什么在得到如下答案后不要致电process
,
-(void)process
{
timestamp=[NSString stringWithFormat:@"%1.f",progressValue];
NSString *contour=@"&bandschema=4";
NSString *url6=[NSString stringWithFormat:@"http://contour.php? callback=contourData%@&type=json×tamp=%@%@",timestamp,timestamp,contour];
NSURL *url1=[NSURL URLWithString:url6];
__weak ASIHTTPRequest *request1 = [ASIHTTPRequest requestWithURL:url1];
[request1 setCompletionBlock:^{
responseString = [request1 responseString];
[self plotPoint:self.responseString];
if (somecondition)//based on some condition to break the chain when needed
[self process];
}];
[request1 setFailedBlock:^{
NSError *error=[request1 error];
NSLog(@"Error: %@", error.localizedDescription);
if (somecondition)//based on some condition to break the chain when needed
[self process];
}];
[request1 startAsynchronous];
}
这样,您可以在获得创建新请求的响应后将1.6保留为确切的时间间隔。