iphone - 从 NSThread 函数调用时,不会调用 NSURLConnection 委托(delegate)

标签 iphone ios objective-c xcode nsurlconnection

我发送了 NSURLConnection 请求,它工作正常。现在我想刷新信息,即从按钮的 IBAction 调用时重新发送 NSURLConnection.Refresh 正在工作。但在 NSThread 方法中不起作用。我该如何解决这个问题。这里的NSThread函数用于运行系统时间。当时间等于凌晨 1:00 时,我想刷新 API。但它并没有调用NSURLConnection的委托(delegate)。

这是 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
}

上面的代码在名为“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委托(delegate)没有被调用。

最佳答案

要调用委托(delegate)方法,您需要将线程的运行循环附加到 NSURLConnection。由于您正在创建一个线程并且没有将 NSURLConnection 附加到线程的 RunLoop,因此连接委托(delegate)方法不会被触发。

这是一个例子:

- (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");
}

关于iphone - 从 NSThread 函数调用时,不会调用 NSURLConnection 委托(delegate),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10537038/

相关文章:

iphone - 响应式设计高达 320 像素?

iphone - 如何以编程方式在 UITableView 后面添加静态背景?

ios - 在 dismissViewController 上重新加载 View (模态)

iphone - 使用 iPhone 摄像头作为亮度传感器

ios - 当视频在 WebView 中播放时,在 iOS (Swift) 中向 AVPlayer 添加 UIButton

IOS:旋转 uiimageview

iphone - iOS 中的自定义转场

ios - UISwipeGestureRecognizer 对角滑动问题

iphone - 如何识别核心数据管理对象模型——哈希?

ios - 我改变观点的方式有问题吗?