2012-03-09 56 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

什麼是循環? – MByD 2012-03-09 09:48:30

+0

我不是。有沒有可能異常地運行虛空? – theShay 2012-03-09 11:11:58

回答

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]; 
     } 
});