Question

I am using performSelector to call URLRequest every couple of second with different timetstamp. However, data processing may take longer than the time I have defined.

  [self performSelector:@selector(process) withObject:nil afterDelay:1.6];

below part shows the method is called

 -(void)process
{
    timestamp=[NSString stringWithFormat:@"%1.f",progressValue];
    NSString *contour=@"&bandschema=4";
    NSString *url6=[NSString stringWithFormat:@"http://contour.php?  callback=contourData%@&type=json&timestamp=%@%@",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];
    }

this part is start point of analyzing data.

-(void)plotPoint:(NSString *)request
{
    NSArray *polygonArray = [[dict  objectForKey:@"data"]valueForKey:@"polygon"];
    NSArray *valleyPolygonArray = [[dict objectForKey:@"valley"]valueForKey:@"polygon"];
    CLLocationCoordinate2D *coords;
}

However sometimes time interval is not enough to get new data especially when internet connection is not good.

Could you guide me please? How could I handle the problem? What is the optimal solution?

Was it helpful?

Solution

Why dont you call process after you get the response as follows,

-(void)process
{
    timestamp=[NSString stringWithFormat:@"%1.f",progressValue];
    NSString *contour=@"&bandschema=4";
    NSString *url6=[NSString stringWithFormat:@"http://contour.php?  callback=contourData%@&type=json&timestamp=%@%@",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];
    }

This way you can keep 1.6 as the exact time interval after getting a response to creating a new request.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top