Asynchronous URL connection objective C

Posted by tweety on Stack Overflow See other posts from Stack Overflow or by tweety
Published on 2014-06-07T03:21:52Z Indexed on 2014/06/07 3:24 UTC
Read the original article Hit count: 199

Filed under:
|
|

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

© Stack Overflow or respective owner

Related posts about objective-c

Related posts about ios7