2012-05-10 4 views
2

NSURLConnection 요청을 보내면 문제가 없습니다. 이제 정보를 새로 고침 (예 : NSURLConnection.Refresh) 버튼의 IBAction에서 호출 할 때 다시 작동하고 싶습니다. 그러나 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 대표가 호출되지 않습니다.

+0

은 내가 하루 종일 보냈다 ... 나에게 도움을 주시기 바랍니다 : 여기에

은 예입니다. 버튼 기능 API 호출에서 작동합니다. 그러나 NSThread에서 작동하지 않습니다. – Madhubalan

+3

대리자 메서드를 호출하려면 runloop를 NSURLConnection에 연결해야합니다. 스레드를 만들고 NSURLConnection을 스레드의 RunLoop에 연결하지 않으므로 연결 대리자 메서드가 실행되지 않습니다. – 0x8badf00d

답변

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"); 
} 
관련 문제