2012-03-09 51 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]; 
     } 
});