从NSThread的函数调用时,不调用NSURLConnection委托

时间:2012-05-10 15:12:23

标签: iphone ios objective-c xcode nsurlconnection

我已发送NSURLConnection请求它正常工作。现在我想刷新信息,即在从IBAction按钮调用时重新发送NSURLConnection.Refresh正在工作。但是不能使用NSThread方法。我该如何解决这个问题。这里NSThread用于运行系统时间。当时间等于凌晨1:00时,我想刷新API。但它并没有调用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
}

上面的代码是in in函数,名为“displays”参数是类的一个实例,它具有所有地方的详细信息。

NSthread功能代码:

- (void) setTimer {    
   //assign current time
    [self countDown];
}

- (void) countDown {
   //count the current time 

   if(hrs==12&& meridian==@"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委托。

1 个答案:

答案 0 :(得分:4)

对于要调用的委托方法,您需要将线程的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");
}