2012-05-10 5 views
2

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 
} 

はあるが、それはすべての場所の詳細情報を持つクラスの1つのインスタンスです。

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呼び出しが機能します。関数 – Madhubalan

+3

と呼ばれるNSThreadでは動作しません。デリゲートメソッドを呼び出すには、runloopをNSURLConnectionに接続する必要があります。スレッドを作成していて、スレッドのRunLoopにNSURLConnectionを付加していないので、接続代理メソッドは起動しません。 – 0x8badf00d

答えて

4

デリゲートメソッドを呼び出すには、スレッドのrunloopをNSURLConnectionにアタッチする必要があります。スレッドを作成していて、スレッドのRunLoopにNSURLConnectionを付加していないので、接続代理メソッドは起動しません。ここで

は一例です。

- (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"); 
} 
関連する問題