2012-03-09 11 views
-2

みなさん、こんにちはを実行している、私はこのコードでasynchourslyループを実行していることならば作成すること:ループ場合asynchoursly

NSString *care = [[NSString alloc] initWithContentsOfURL:[NSURL URLWithString:@"http://localhost/untitled.txt"]]; 
if (care == @"ONRED") { //untitled.txt = ONRED 
    [red_on setHidden:NO]; 
    [red_off setHidden:YES]; 
} 

私はループのようなif文を実行することができますどのように?

+1

if-loopとは何ですか? – MByD

+0

私はいません。ボイドを非同期的に実行するためのvayがありますか? – theShay

答えて

1

私は右のあなたを取得する場合、あなたはこの方法では、これらのステートメントを入れて、performSelectorInBackgroundを呼び出すことができます。

(void)asyncMethod { 
    NSString *care = [[NSString alloc] initWithContentsOfURL:[NSURL URLWithString:@"http://localhost/untitled.txt"]]; 
    if (care == @"ONRED") { //untitled.txt = ONRED 
     [red_on setHidden:NO]; 
     [red_off setHidden:YES]; 
    } 
} 

// in some other method 
[self performSelectorInBackground:@selector(asyncMethod) withObject:nil]; 

別のオプションは、グランドセントラルディスパッチ(this answerで説明したように)使用することです:

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 
             (unsigned long)NULL), ^(void) { 
    NSString *care = [[NSString alloc] initWithContentsOfURL:[NSURL URLWithString:@"http://localhost/untitled.txt"]]; 
     if (care == @"ONRED") { //untitled.txt = ONRED 
      [red_on setHidden:NO]; 
      [red_off setHidden:YES]; 
     } 
}); 
関連する問題