【发布时间】:2011-08-10 23:22:24
【问题描述】:
我目前正在尝试在发出异步请求时设置 20 秒的时间。
遇到的问题是 NSURLconnection 在主线程上运行,因此,如果我运行 NSTIMER 来计算经过的秒数,它永远不会触发选择器,因为 NSURLconnection 阻塞了主线程。我可能可以在不同的线程上运行 NSURLconnection,因为它是线程安全的,但是我有一些奇怪的问题,比如我的代表没有被调用等等。任何帮助都非常感谢。
下面是我的片段:
NSURL *requestURL = [NSURL URLWithString:SERVER];
NSMutableURLRequest *req = [[[NSMutableURLRequest alloc] initWithURL:requestURL] autorelease];
theConnection = [[NSURLConnection alloc]initWithRequest:req delegate:self];
if (!timeoutTimer) {
NSLog(@"Create timer");
timeoutTimer = [NSTimer scheduledTimerWithTimeInterval:TIMEOUT target:self selector:@selector(cancelURLConnection) userInfo:nil repeats:YES];
}
【问题讨论】:
标签: iphone objective-c nsurlconnection nstimer nsthread