我使用HTTP POST方法创建了一个异步URL连接来调用Web服务。在我ping网络后,我在完成处理程序中设置了NSTimerInterval。我的问题是当我试图在视图控制器上显示时间时,它没有及时做。我知道块存储在堆中并在以后随时执行,这可能就是我没有得到快速回答的原因。我想知道还有其他办法吗?
提前致谢。
我的代码:
__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];