Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/99.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 从NSThread';调用时不调用NSURLConnection委托;s函数_Iphone_Ios_Objective C_Xcode_Nsurlconnection - Fatal编程技术网

Iphone 从NSThread';调用时不调用NSURLConnection委托;s函数

Iphone 从NSThread';调用时不调用NSURLConnection委托;s函数,iphone,ios,objective-c,xcode,nsurlconnection,Iphone,Ios,Objective C,Xcode,Nsurlconnection,我发送了NSURLConnection请求,请求它工作正常。现在我想刷新信息,即重新发送NSURLConnection。当从按钮的iAction调用时,refresh正在工作。但无法从NSThread方法工作。我如何解决这个问题。这里是运行系统时间的NSThread函数。当时间等于凌晨1:00时,我想刷新API。但它不是调用NSURLConnection的委托 这是NSURL连接代码: -(void)displays:(model *)place { NSString *strs=[@"ht

我发送了
NSURLConnection
请求,请求它工作正常。现在我想刷新信息,即重新发送
NSURLConnection。当从按钮的iAction调用时,refresh
正在工作。但无法从
NSThread
方法工作。我如何解决这个问题。这里是运行系统时间的
NSThread
函数。当时间等于凌晨1:00时,我想刷新API。但它不是调用
NSURLConnection
的委托

这是NSURL连接代码:

-(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
}
- (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];
}
上面的代码在名为“displays”的函数中,参数是类的一个实例,它具有所有位置详细信息

NSthread函数代码:

-(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
}
- (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 assigned,但未调用委托。

要调用委托方法,需要将线程的运行循环附加到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");
}

对于要调用的委托方法,您需要将线程的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 called function中不起作用要调用委托方法,需要将runloop附加到NSURLConnection。由于您正在创建一个线程,并且没有将NSURLConnection附加到线程的RunLoop,因此不会触发连接委托方法。请帮助我。。。我花了一整天的时间。从按钮函数API调用工作。但在NSThread called function中不起作用要调用委托方法,需要将runloop附加到NSURLConnection。因为您正在创建一个线程,并且没有将NSURLConnection附加到线程的RunLoop,所以不会触发连接委托方法。