Asynchronous URL connection objective C
- by tweety
I created an asynchronous URL connection to call a web service using HTTP POST method. after I am pinging the web i set an NSTimerInterval in the completion handler. my problem is when I'm trying to display the time on the view controller it is not doing promptly. I know block is stored in the heap and gets executed later on anytime and probably that's why i'm not getting prompt answer. I was wondering is there any other way to do this?
Thanks in advance.
my code:
__block NSDate *start= [NSDate date];
__block NSDate *end;
__block double miliseconds;
__block NSTimeInterval time;
[NSURLConnection sendAsynchronousRequest:urlRequest queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
if([data length]==0 && error==nil){
end=[NSDate date];
time=[end timeIntervalSinceDate:start];
NSLog(@"Successfully Pinged");
miliseconds = time;
// calling a method to display ping time
[self label:miliseconds];
}
-(void) label:(double) mili{
double miliseconds=mili*1000;
self.timeDisplay.text=[NSString stringWithFormat:@"Time: %.3f ms", miliseconds];