当NSThread的函数调用时,NSURLConnection委托没有被调用
问题描述:
我发送NSURLConnection
请求它工作正常。现在我想刷新信息,即当从IBAction按钮调用时重新发送NSURLConnection.Refresh
正在工作。但从NSThread
方法不工作。我如何解决这个问题。这里用NSThread
函数来运行系统时间。如果时间等于凌晨1点,我想刷新API。但它不叫NSURLConnection
的代表。当NSThread的函数调用时,NSURLConnection委托没有被调用
这是NSURLConnection的代码:
-(void)displays:(model *)place
{
NSString *strs=[@"http://www.earthtools.org/timezone-1.1/" stringByAppendingString:[NSString stringWithFormat:@"%@/%@",place.latitude,place.longitude]];
NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:[NSURL URLWithString:strs]];
NSURLConnection *reqTimeZone=[NSURLConnection connectionWithRequest:request delegate:self];
[reqTimeZone start]; //here request not get start
}
上面的代码在调用的函数“显示”的说法是类的一个实例它拥有所有地点的详细信息。
NSthread功能代码:
- (void) setTimer {
//assign current time
[self countDown];
}
- (void) countDown {
//count the current time
if(hrs==12&& [email protected]"pm")
[self display:(placedetails)];//it calls the displays function but NSURLConnection is not get start.
[NSThread detachNewThreadSelector:@selector(setTimer) toTarget:self withObject:nil];
}
上面显示函数被调用分配placedetails但NSURLConnection
委托不叫。
答
对于要调用的委托方法,您需要将线程的runloop附加到NSURLConnection。由于您正在创建一个线程,并且不将NSURLConnection附加到线程的RunLoop,因此连接委托方法不会被触发。
下面是一个例子:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// I am creating a button and adding it to viewController's view
UIButton *bttn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[bttn setFrame:CGRectMake(100.0f, 200.0f, 120.0f, 50.0f)];
[bttn setTitle:@"Download" forState:UIControlStateNormal];
[bttn addTarget:self action:@selector(spawnThreadForDownload) forControlEvents:UIControlEventTouchUpInside];
[[self view] addSubview:bttn];
}
- (void)spawnThreadForDownload
{
[NSThread detachNewThreadSelector:@selector(downloadAndParse) toTarget:self withObject:nil];
}
- (void)downloadAndParse
{
@autoreleasepool {
NSURL *url = [NSURL URLWithString:@"http://apple.com"];
NSURLRequest *req = [NSURLRequest requestWithURL:url
cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData
timeoutInterval:20.0f];
NSURLConnection *conn = [NSURLConnection connectionWithRequest:req delegate:self];
// Run the currentRunLoop of your thread (Every thread comes with its own RunLoop)
[[NSRunLoop currentRunLoop] run];
// Schedule your connection to run on threads runLoop.
[conn scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
}
}
// NSURLConnectionDelegate methods
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
NSLog(@"Connection failed with error: %@",[error localizedDescription]);
}
// NSURLConnectionDataDelegate methods
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSLog(@"Connection finished downloading");
}
请帮我...我花了整整一天。从按钮功能API调用起作用。但不能在NSThread中调用函数 – Madhubalan
对于要调用的委托方法,您需要将runloop附加到NSURLConnection。由于您正在创建一个线程,并且不将NSURLConnection附加到线程的RunLoop,因此连接委托方法不会被触发。 – 0x8badf00d